[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help parsing from html tags

Posted on 2011-04-28
3
Medium Priority
?
187 Views
Last Modified: 2012-05-11
I have a field, called bodytext, in a MySQL database.  It's a mediumtext field.  In the paragraphs of text there are links scattered, formatted like this:

<link news/single-news-page/article/airbus-opens-a320-line-in-tianjin-18102/ _blank>A320 final assembly</link>

Open in new window


Note the last section of the link (18102).  They're all formatted like that, with the ID last - though not always 5 digits, they start at the low 20's.  

The new link needs to be:
<a href="?q=node/18102">A320 final assembly</a>

Open in new window


Yes, I'm moving from an old CMS into Drupal.
0
Comment
Question by:michaelgiaimo
  • 2
3 Comments
 

Author Comment

by:michaelgiaimo
ID: 35486895
I should add the link isn't the only thing in the field.  I need to keep all surrounding text and just update the link.
0
 
LVL 8

Accepted Solution

by:
ropenner earned 2000 total points
ID: 35487306
this does the job if the links are always in that format... necessary parts are shown below by using asterisks for the unnecessary parts.

<LINK ************-123456/ _blank>******************</LINK>
<?PHP

/* 
this
<link news/single-news-page/article/airbus-opens-a320-line-in-tianjin-18102/ _blank>A320 final assembly</link>
to this
<a href="?q=node/18102">A320 final assembly</a>
and leave the rest the same
*/

$CODE =<<< endofcode
<object width="425" height="344">
<param name="movie" value="http://www.youtube.com/v/YOU_TUBE_IDENTIFIER&hl=en_US&fs=1&"></param>
<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
<HR>
<link news/single-news-page/article/airbus-opens-a320-line-in-tianjin-18102/ _blank>A320 final assembly</link>
<HR>
blah blah
</embed>
</object>
endofcode;


// START
$output = preg_replace("/<LINK .*-(\d*)\/ _blank>(.*)<\/LINK>/i","<A HREF=\"?q=node/\\1\">\\2</A>",$CODE);

print "$output";
?>

Open in new window

0
 

Author Closing Comment

by:michaelgiaimo
ID: 35514601
Thanks, worked.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question