preg_match parsing problems
| From: | Jeremy Jacobs | Date: | Sat, 25 Nov 2000 23:36:15 +0000 |
| Subject: | preg_match parsing problems | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-27198@lists.php.net to get a copy of this message | ||
I'm trying to write a php program that will parse text in the following
format (items to be extracted are capitalized):
<a href="URL_NAME" blah blah mouseover Javascript code>TITLE</a><br>
DESCRIPTION OF TITLE.<p>
The way I have it set up, the first page simply asks you how many of
these "<a href=..." entries you want to enter, and assigns that number
to $entries. The second page creates textareas in a form with the
following code ($ri stands for "rapid insert"):
<?php
for($ri = 1; $ri <= $entries; $ri++) {
echo "<textarea cols=\"70\" rows=\"3\"
wrap=\"virtual\"
name=\"subject[$ri]\">\n";
echo "</textarea><p>\n\n";
}
?>
so if $entries = 4, you end up with text dumped into $subject[1],
$subject[2], $subject[3], and $subject[4]. It also asks you for a URL
ID (one ID for all of them), assigned to $url_id_submit. Then $entries
gets passed on to the next page in a hidden input.
Ultimately, this information going to go into a database, but for
debugging purposes I'm just having it print out, and this is where I'm
getting stuck. Here's the code I'm using:
<?php
$pattern_url = "/
^
.{9}
(\w+)
\.
(\w+)
\"
.+
>
(.+)
<\/a>
<br>
\W
(.+)
<p>
/x";
for($ri = 1; $ri <= $entries; $ri++) {
preg_match($pattern_url, $subject[$ri], $matches);
$url[$ri] = "$matches[1]" . "." . "$matches[2]";
$url_title[$ri] = "$matches[3]";
$url_desc[$ri] = "$matches[4]";
$url_name[$ri] = "$matches[1]";
$url_id[$ri] = "$url_id_submit";
echo "$url[$ri]<br>";
echo "$url_title[$ri]<br>";
echo "$url_desc[$ri]<br>";
echo "$url_name[$ri]<br>";
echo "$url_id[$ri]<p>";
}
?>
But all I get out of this is:
.
music
repeated 4 times ($url_id_submit was set to "music", no pun intended).
Any suggestions?