Jump to content

Archived

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

esiason14

Quick regex / bbcode

Recommended Posts

I'm just trying to match something like this:

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

and replace it with

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

any help would be appreciated

Share this post


Link to post
Share on other sites
[code]
<?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);
?>
[/code]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
The same code will work since it lacks BOL (beginning of line) or EOL (end of line) anchors.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
The code is always replacing the string[tt] [ url=http://www.mydomain.com/phpBB2/viewtopic.php?t=".$topic_id[$i]."]Read more...[/url ] [/tt] as instructed; I'm not sure where your other information comes into play.

Share this post


Link to post
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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[code]
preg_match('%(?<=<span class="postbody">)(.+?)(?=</ br>)%s', $string, $matches);
[/code]

Share this post


Link to post
Share on other sites

×

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.