Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP] Regex driving me crazy

duck you, I'm a dragon
Loyal Member
Joined
Apr 29, 2005
Messages
6,407
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?
 
Custom Title Activated
Loyal Member
Joined
Jun 28, 2007
Messages
2,986
Reaction score
3
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?)
 
duck you, I'm a dragon
Loyal Member
Joined
Apr 29, 2005
Messages
6,407
Reaction score
130
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>"
 
Custom Title Activated
Loyal Member
Joined
Jun 28, 2007
Messages
2,986
Reaction score
3
Wait...do you want to make a regex that will get rid of empty tags?
 
duck you, I'm a dragon
Loyal Member
Joined
Apr 29, 2005
Messages
6,407
Reaction score
130
Nope, just match tags which have a certain title, "pie.author" in this case.
 
Experienced Elementalist
Joined
Dec 27, 2006
Messages
288
Reaction score
4
Try something like...

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

That probably won't work, but you get the idea.
 
Newbie Spellweaver
Joined
Jul 2, 2008
Messages
49
Reaction score
2
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">')
 
duck you, I'm a dragon
Loyal Member
Joined
Apr 29, 2005
Messages
6,407
Reaction score
130
Hmm, I currently have the following regex

PHP:
preg_match("/\<.*title\=\\\"pie.author\\\".*\>[\w\s]*\<\/w\>/is", $content, $test);

But it still doesn't work.
 
Custom Title Activated
Loyal Member
Joined
Jun 28, 2007
Messages
2,986
Reaction score
3
Yeah, at least the ending tag is incorrect:

\<\/w\>

Should be:

\<\/.*\>
 
duck you, I'm a dragon
Loyal Member
Joined
Apr 29, 2005
Messages
6,407
Reaction score
130
It now outputs the entire contents of $str. o_O Which I don't get as it should only match tags which have "pie.author" as title.
 
duck you, I'm a dragon
Loyal Member
Joined
Apr 29, 2005
Messages
6,407
Reaction score
130
After some help from the kind folks over at phpfreakz, I finally solved it:

PHP:
$spattern = sprintf('#<([a-z0-9]+)[^>]title=\"%s\"[^<]*>.*</\\1>#','pie.author');
                                                        
preg_match($spattern, $str, $aMatch);
 
Back
Top