[PHP] Regex driving me crazy

Joined
Apr 29, 2005
Messages
6,400
Reaction score
130
So I want to create a regex which will look up a specific tag in a source code and the only thing you know of said tag is that it has a title (Which you know)

let me give an example, you have these three tags:

Code:
<h1 title=\"pie.subject\"></h1>
<h2 title=\"pie.author\">asdasdasd</h2>
<div title=\"pie.text(text)\">
</div>

Now what regex should I use that it returns "<h2 title=\"pie.author\">asdasdasd</h2>"?

I've been busting my balls on this but I just can't seem to figure it out.

Any suggestions?
 
I don't get it. What do you want to transform into what?

(it can be quite tricky to get regex working when the to-be-matched string has backslashes...is that what you mean?)
 
OH wait, I should have mentioned I'm working with preg_match, basically what I want is this:

PHP:
$str= "<h1 title=\"pie.subject\"></h1>
<h2 title=\"pie.author\">asdasdasd</h2>
<div title=\"pie.text(text)\">
</div>"

preg_match("/regex/", $str, $var)

Where $var[x] contains "<h2 title=\"pie.author\">asdasdasd</h2>"
 
Wait...do you want to make a regex that will get rid of empty tags?
 
Try something like...

Code:
<[\w\s]+title="pie.author"[\w\s"=]*>[\w\s]*</\w>

That probably won't work, but you get the idea.
 
I'd suggest duplicating the original HTML source string and looping through it with eregi and deleting the match so it doesn't get looped again.
PHP:
$tagString = $my_old_string;
$resultTagArray = array();
while(eregi('<[a-zA-Z ]+ title\=\"([a-zA-Z0-9._)"[a-zA-Z ]+ >', $tagString, $regexMatch) {
   #remove the tag from the string list
   $tagString = str_replace('', $regexMatch[0], $tagString);
   #add the complete match to the array
   array_push($resultTagArray, $regexMatch[0]);
}
#=> array([0] => '<div title="super_title">')
 
Yeah, at least the ending tag is incorrect:

\<\/w\>

Should be:

\<\/.*\>
 
Back