regex help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

regex help

Post by malcolmboston »

i need help with regex strings to find the information marked XXX from the following source code

Code: Select all

1) <span class='r'>XXX</span>
2) <td class=ProductPrice>XXX</td>
3) <h3 class="SpecialPrice" style="display:inline; padding-bottom:0px;">XXX</h3>
4) <font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b>XXX</b></font>
5) <span class="price">XXX</span>
ive always thought regex was a nigtmare so any help, explanation on how it works (no rtfm please, ive done that 100x now)

thanks in advance
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post by yum-jelly »

Just make function() that will build your regex(s) based on the patterns you want, this way you can reuse the same function over and over again.

Code: Select all

<?

// the patterns to find, values to get (1 per line, use _GET_ for the value you want)

$junk = '<span class=\'r\'>_GET_</span>
<td class=ProductPrice>_GET_</td>
<h3 class="SpecialPrice" style="display:inline; padding-bottom:0px;">_GET_</h3>
<font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b>_GET_</b></font>
<span class="price">_GET_</span>';

// example string that we will get our values from...

$string = '1) <span class=\'r\'>It was a very sunny day!</span>
2) <td class=ProductPrice>The vacation cost big bucks.</td>
3) <h3 class="SpecialPrice" style="display:inline; padding-bottom:0px;">There is no special price!!!</h3>
4) <font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b>Visit Cat Man Do</b></font>
5) <span class="price">Way to much</span> ';

// the regex builder

function make_regex ( $in )
{
	$out = array ();

	$multi = preg_split ( '/\r?\n/', $in );

	foreach ( $multi AS $value )
	{
		$parts = explode ( '_GET_', $value );

		for ( $x = 0; $x < sizeof ( $parts ); $x++ )
		{
			$parts[$x] = preg_quote ( $parts[$x], '/' );
		}

		$out[] = array ( '/' . implode ( '(.*?)', $parts ) . '/is', 1 );
	}

	return ( $out );
}

// get the values

// the data container

$data = array ();

$regex = make_regex ( $junk );

foreach ( $regex AS $k => $v )
{
	preg_match_all ( $v[0], $string, $found );

	$data[] = $found[$v[1]];
}

// show the result

print_r ( $data );

?>

yj!
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

<-- confused
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post by jwalsh »

Couldn't you just strip_tags() to remove the HTML code, then explode by newlines /n ?
Post Reply