preg_match output

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

preg_match output

Post by rsmarsha »

I have the following code :

Code: Select all

 
preg_match('/<nobr[>][a-zA-Z]+<\/nobr><\/td>[\s]*<td class="fieldValue">[a-zA-z]+<\/td>/', '<nobr>Server</nobr></td> <td class="fieldValue">Name</td>other text not needed', $matches);
print_r($matches);
 
Which prints out "Array ( [0] => Server Name ) "

Is it possible to have it only pick out the Name part of the match? Or would i just have to use further functions to get that. The actual string to match will be a variable containing a html page and the "Name" section will vary.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: preg_match output

Post by prometheuzz »

Try this:

Code: Select all

 
preg_match(
    '/<nobr[>][a-zA-Z]+<\/nobr><\/td>[\s]*<td class="fieldValue">([a-zA-Z]+)<\/td>/', 
    '<nobr>SERVER</nobr></td> <td class="fieldValue">NAME</td>other text not needed', 
    $matches);
print_r($matches);
print("\nName only: " . $matches[1] . "\n");
 
Post Reply