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


Help parsing from html tags

Posted on 2011-04-28
Medium Priority
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.
Question by:michaelgiaimo
  • 2

Author Comment

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.

Accepted Solution

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>

<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>
<link news/single-news-page/article/airbus-opens-a320-line-in-tianjin-18102/ _blank>A320 final assembly</link>
blah blah

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

print "$output";

Open in new window


Author Closing Comment

ID: 35514601
Thanks, worked.

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