Replacing some text with varying number with a link

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
runder
Forum Newbie
Posts: 2
Joined: Fri Feb 12, 2010 11:32 am

Replacing some text with varying number with a link

Post by runder »

I need to change a text in a PHP page from this:

Code: Select all

 Ticket #23 
to this:

Code: Select all

 <a href="http://www.somelink.com/ticket/23">Ticket #23</a> 
where the actual number (in this case, 23) is a varying number. Meaning, if the ticket was #87 (Ticket #87), the actual output would have been

Code: Select all

 <a href="http://www.somelink.com/ticket/87">Ticket #87</a> 
I know I need to work with preg_replace, but it's been hours I still can't figure it out :') It's mainly storing the number in a variable to be reused with some sort of variable string ($1, $2) that poses a lot of problem to me when I'm trying to figure this out.

Any help is appreciated. Thanks :)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Replacing some text with varying number with a link

Post by josh »

From my site http://www.vehiclefits.com/changelog.php

Code: Select all

 
$ob = preg_replace( '#([0-9]+):#', '<a target="_blank" href="http://tracker.ne8.net/view.php?id=$1">$1</a>:', $ob );
Consider it public domain now ;-)
runder
Forum Newbie
Posts: 2
Joined: Fri Feb 12, 2010 11:32 am

Re: Replacing some text with varying number with a link

Post by runder »

Oh wow, I love you :) the programmer way of course ^_^;

Thank you very much, Josh.
Post Reply