Help parsing from html tags

Posted on 2011-04-28
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

    Author Comment

    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.
    LVL 8

    Accepted Solution

    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=""></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
    // START
    $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

    Thanks, worked.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now