Jump to content

Quick regex / bbcode


esiason14

Recommended Posts

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

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

and replace it with

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

any help would be appreciated
[/quote]
Link to comment
https://forums.phpfreaks.com/topic/19322-quick-regex-bbcode/#findComment-86086
Share on other sites

This is what I have:

[code]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>";[/code]


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

[code]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...[/code]


..returns this:

[code]Manning likes addition of Addai (this is the title of the post)
Read more...[/code]
Link to comment
https://forums.phpfreaks.com/topic/19322-quick-regex-bbcode/#findComment-86151
Share on other sites

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.

[code]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>[/code]
Link to comment
https://forums.phpfreaks.com/topic/19322-quick-regex-bbcode/#findComment-86439
Share on other sites

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 [b]$bbcode[/b] from:

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

to:

[code]
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]
[/code]

You should get the desired result.
Link to comment
https://forums.phpfreaks.com/topic/19322-quick-regex-bbcode/#findComment-86471
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.