Jump to content


Photo

Quick regex / bbcode


  • Please log in to reply
10 replies to this topic

#1 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 01 September 2006 - 05:47 AM

I'm just trying to match something like this:

[url=http://mydomain.com/nfl/players/playerpage/1234/rss]Read more...[/url]

and replace it with

<a href="http://www.mydomain.com/whatever.php">Read more...</a>

any help would be appreciated

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 01 September 2006 - 01:45 PM

<?php
	$bbcode = '[url=http://mydomain.com/nfl/players/playerpage/1234/rss]Read more...[/url]';
	echo preg_replace('%\[url=([^]]+)\]([^[]+)\[/url\]%', '<a href="\1">\2</a>', $bbcode);
?>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 05 September 2006 - 12:01 AM

Hi effigy,
Thanks for the response. That does work perfectly. One followup question though:

What if there is a block of text prior to the url that I want to replace. how can I match that and show it along with the new, reformatted link. The block of text is always followed by a double line break

A whole bunch of text here. Blah blah. SDSdsdsad. 
[url=http://mydomain.com/nfl/players/playerpage/1234/rss]Read more...[/url]

and replace it with

A whole bunch of text here. Blah blah. SDSdsdsad.
 <a href="http://www.mydomain.com/whatever.php">Read more...</a>

any help would be appreciated
[/quote]

#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 05 September 2006 - 04:03 AM

The same code will work since it lacks BOL (beginning of line) or EOL (end of line) anchors.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 05 September 2006 - 04:13 AM

This is what I have:

echo "<br><table align=\"center\" width=\"500\"><tr bgcolor=\"#000066\"><td style=\"font-family: Arial,Helvetica,sans-serif; font-weight: bold; font-size:12px; color:#ffffff; text-decoration:none; \">  $firstname $lastname in the news</td></tr>";			
  for($i=0;$i<$count;$i++)
  {	
	$bbcode = "[url=http://www.mydomain.com/phpBB2/viewtopic.php?t=".$topic_id[$i]."]Read more...[/url]";
	$newurl = preg_replace('%\[url=([^]]+)\]([^[]+)\[/url\]%', '<a href="\1">\2</a>', $bbcode);
echo "<tr><td><b><u>".$title[$i]."</u></b></td></tr><tr><td>".$newurl."</td></tr>";
}
echo "</table>";


For a post that looks like this, which I'm trying to match:

Colts QB Peyton Manning said he likes the team's decision to draft Joseph Addai to help replace 
Edgerrin James. "I'm biased toward Southeastern Conference players. I think he's been well battle-tested," Manning said. "He's going to be able to come in and help us. Dominic Rhodes will play as well, but everybody else is going to have to raise their level of play."

Read more...


..returns this:

Manning likes addition of Addai (this is the title of the post)
Read more...


#6 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 05 September 2006 - 04:56 AM

The code is always replacing the string [ url=http://www.mydomain.com/phpBB2/viewtopic.php?t=".$topic_id[$i]."]Read more...[/url ] as instructed; I'm not sure where your other information comes into play.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#7 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 05 September 2006 - 03:15 PM

I'm actually trying to match the text before AND the "read more" link.....not just the link. Currently, I can return only the "Read more" link with what you posted before.  The whole thing that I am trying to match looks like this.

In two preseason games, Carson Palmer (knee) went 13-of-20 for 173 yards and four touchdowns, leaving him with a gaudy passer rating of 131.9. Even though he's still working some out some kinks, he's good enough at this point to have the Bengals thinking of another title. &quot;We were fortunate to finish out the preseason undefeated,&quot; Palmer said. &quot;Hopefully this momentum will carry us through the first four games, into the bye, and on through the rest of the season.&quot;

<br />

<br />
<a href="http://mydomain.com/players/playerpage/396173/rss" target="_blank" class="postlink">Read more...</a>


#8 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 05 September 2006 - 03:35 PM

Whatever you want to match against, use it as your match variable. As I mentioned before, you're only matching against an url string in your example.

If you change $bbcode from:

[url=http://www.mydomain.com/phpBB2/viewtopic.php?t=".$topic_id[$i]."]Read more...[/url]

to:

In two preseason games, Carson Palmer (knee) went 13-of-20 for 173 yards and four touchdowns, leaving him with a gaudy passer rating of 131.9. Even though he's still working some out some kinks, he's good enough at this point to have the Bengals thinking of another title. &quot;We were fortunate to finish out the preseason undefeated,&quot; Palmer said. &quot;Hopefully this momentum will carry us through the first four games, into the bye, and on through the rest of the season.&quot;
<br />
<br />
[url=http://www.mydomain.com/phpBB2/viewtopic.php?t=".$topic_id[$i]."]Read more...[/url]

You should get the desired result.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#9 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 05 September 2006 - 03:50 PM

Ok, I gotcha. I'm sorry to be a pain, but the text is different everytime. How would I match the text between the <span class="postbody"> and the first </ br> tag.

I promise this is my last question on this. Thanks again

#10 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 05 September 2006 - 04:13 PM

preg_match('%(?<=<span class="postbody">)(.+?)(?=</ br>)%s', $string, $matches);

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#11 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 06 September 2006 - 10:13 PM

Thanks, effigy. Everything is working great. You da man!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users