0

I don't know if this is possible but I have a piece of plain text which always includes:

http://www.royalmail/portal/etc/etc/track.php?trackNumber=123345323 

When dragged to my page from the database this obviously does not render as a link. Is there any way to select this piece of text (ie via the http://) and at the most basic wrap it with anchor tags - or more complexly - retrieve the address, wrap the address with anchor tags, and change the original http:// text to something more understandable such as 'Track Parcel'

It should be noted that there could be numerous tracking references looped out at once.

My containing HTML is as follows

<div class="message_holder"> <div class="mess_head" style="width:110px;"> <p><strong>Sender: </strong><? echo $row11['sender'];?></p> </div> <div class="mess_head" style="width:450px;"> <p><strong>Subject: </strong><span class="red"><? echo $row11['subject'];?></span></p> </div> <div class="mess_head" style="width:150px;"> <p><strong>Date Sent: </strong><? echo date('d/m/Y, H.ia', $row11['date_sent']);?></p> </div> <div class="mess_head" style="width:200px;"> <p><strong>Message ID: </strong><? echo $row11['message_id'];?></p> </div> <div class="mess_body"> <p><? echo html_entity_decode($row11['message']);?></p> </div> </div> 

The plain text links will all show up in the 'mess_body' class. I have tried using html_entity_decode() but this did not do the trick. If there is an easy way to do this via PHP rather than JQuery it could be simpler.

2
  • You should do this using PHP, not Javascript. Why can't you just do <a href="<? echo html_entity_decode($row11['message']);?>">Track Parcel</a>? Commented Jan 13, 2013 at 9:57
  • The link reference is contained within other plain text therefore it needs to be identified as a link. Commented Jan 13, 2013 at 10:33

1 Answer 1

3

I would do it with PHP:

$text = preg_replace( '@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', /* or <a href="$1">Your TEXT</a> */ $text ); 

If I guess right, the url can be in your message, so your code should go like this:

<?php echo preg_replace( '@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@smi', '<a href="$1">$1</a>', html_entity_decode($row11['message']) ); ?> 
Sign up to request clarification or add additional context in comments.

3 Comments

This makes it simpler than using my jquery idea but I can't seem to get your example working in my code - will continue to mess with this - thanks
@Sideshow added an example for your code. Do you get any kind of error, or could you provide an example message which can be used as a test case?
The script runs fine - an example message is as follows:Tracking Number: BY281883295GB The above Royal Mail Tracking Number has been allocated for Appointment ID: 1357901511AeA It may take a couple of hours before the tracking number becomes live on the Royal Mail website. Follow the link below for Tracking details: track2.royalmail.com/portal/rm/track?trackNumber=11111111111

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.