Jump to content

preg_match_all


megafu

Recommended Posts

hi i need some help with this regular expression stuff. :(

 

 

i want to get the userID . but i cant manage to get the regular the right way . :(

 

thanks for any help  ;D

 

here is the code i have:

//<a class="" href="/user/userID" onMouseover="irrelevantstuff" onMouseout="irrelevantstuff">


function getInfo($var) { 
$preg = '*href="/user/*+"*';
preg_match_all(trim($preg), $var, $out);
$keys = $out[1];

if ($keys == NULL)
   {
     die('No Match Found!');
   }

  return $keys[0];
}

 

Link to comment
https://forums.phpfreaks.com/topic/37527-preg_match_all/
Share on other sites

Based on what you've given--the href attribute always uses double quotes--the code below will work. It needs tweaking if you want to match single quotes or no quotes at all.

 

<pre>
<?php
$string = <<<STR
	<a class="" href="/user/bob" onMouseover="irrelevantstuff" onMouseout="irrelevantstuff">
	<a class="" href="/user/suzy1" onMouseover="irrelevantstuff" onMouseout="irrelevantstuff">
STR;
preg_match_all('%(?<=href="/user/)[^"]+%', $string, $matches);
print_r($matches);
?>
</pre>

 

See the links in my signature for more information.

Link to comment
https://forums.phpfreaks.com/topic/37527-preg_match_all/#findComment-179434
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.