PHP

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Regex to wrap <a href=""> tag around a <p> tag

    3 answers - 1252 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hi,
    I am trying to read the contents of a file into a string and wrap an <a
    href=""tag around every <ptag and output it to the browser. This is how
    far I have got:
    // Get file contents
    $file_contents = file_get_contents($file);
    // Replace <ptags
    $file_contents = preg_replace('/^<p\>[a-z][0-9]<\/p>$/', '/^<a
    href="edit_paragraph&text="">\<p>[a-z][0-9]<\/p><\/a>$/', $file_contents);
    // to browser
    echo $file_contents;
    I have two problems.
    1. - The regex doesn't work!
    2. - I need to add the <ptags and all contents to the link
    Here is an example
    <html>
    <body>
    <p>Here is a paragraph</p>
    <p>Here is another paragraph</p>
    </body>
    </html>
    would become
    <html>
    <body>
    <a href="edit_paragraph&text="<p>Here is a paragraph</p>"><p>Here is a
    paragraph</p></a>
    <a href="edit_paragraph&text="<p>Here is another paragraph</p>"><p>Here is
    another paragraph</p></a>
    </body>
    </html>
    Any advice would be greatly appreciated
  • No.1 | | 1838 bytes | |

    The second parameter to preg_replace is the replacement string (with
    optional backreferences), not another patern.

    Use '/<p\>(.*)(?=<\/p>)/' for patern, '<a
    href="edit_paragraph&text=$1"><p>$1' for replacement string, however,
    this does not urlencode the text parameter. You can use
    preg_replace_callback instead of preg_replace to encode it in the
    callback function.

    This won't work for long paragraphs because the lenght of GET request is
    limited. Change it to use forms instead.

    Shaun wrote:
    Hi,

    I am trying to read the contents of a file into a string and wrap an <a
    href=""tag around every <ptag and output it to the browser. This is how
    far I have got:

    // Get file contents
    $file_contents = file_get_contents($file);
    // Replace <ptags
    $file_contents = preg_replace('/^<p\>[a-z][0-9]<\/p>$/', '/^<a
    href="edit_paragraph&text="">\<p>[a-z][0-9]<\/p><\/a>$/', $file_contents);
    // to browser
    echo $file_contents;

    I have two problems.

    1. - The regex doesn't work!
    2. - I need to add the <ptags and all contents to the link

    Here is an example

    <html>
    <body>
    <p>Here is a paragraph</p>
    <p>Here is another paragraph</p>
    </body>
    </html>

    would become

    <html>
    <body>
    <a href="edit_paragraph&text="<p>Here is a paragraph</p>"><p>Here is a
    paragraph</p></a>
    <a href="edit_paragraph&text="<p>Here is another paragraph</p>"><p>Here is
    another paragraph</p></a>
    </body>
    </html>

    Any advice would be greatly appreciated
  • No.2 | | 2661 bytes | |

    Hi M,

    Thanks for your help, the code works fine except if there is a line break in
    the html, for example
    this works

    <p>test</p>

    But this doesnt

    <p>test
    </p>

    Any ideas?

    "M" <lists (AT) gazelasport (DOT) skwrote in message
    news:438EAD90.1040601 (AT) gazelasport (DOT) sk
    The second parameter to preg_replace is the replacement string (with
    optional backreferences), not another patern.

    Use '/<p\>(.*)(?=<\/p>)/' for patern, '<a
    href="edit_paragraph&text=$1"><p>$1' for replacement string, however, this
    does not urlencode the text parameter. You can use preg_replace_callback
    instead of preg_replace to encode it in the callback function.

    This won't work for long paragraphs because the lenght of GET request is
    limited. Change it to use forms instead.

    Shaun wrote:
    >Hi,
    >>

    >I am trying to read the contents of a file into a string and wrap an <a
    >href=""tag around every <ptag and output it to the browser. This is
    >how far I have got:
    >>

    >// Get file contents
    >$file_contents = file_get_contents($file);
    >// Replace <ptags
    >$file_contents = preg_replace('/^<p\>[a-z][0-9]<\/p>$/', '/^<a
    >href="edit_paragraph&text="">\<p>[a-z][0-9]<\/p><\/a>$/',
    >$file_contents);
    >// to browser
    >echo $file_contents;
    >>

    >I have two problems.
    >>

    >1. - The regex doesn't work!
    >2. - I need to add the <ptags and all contents to the link
    >>

    >Here is an example
    >>

    ><html>
    ><body>
    ><p>Here is a paragraph</p>
    ><p>Here is another paragraph</p>
    ></body>
    ></html>
    >>

    >would become
    >>

    ><html>
    ><body>
    ><a href="edit_paragraph&text="<p>Here is a paragraph</p>"><p>Here is a
    >paragraph</p></a>
    ><a href="edit_paragraph&text="<p>Here is another paragraph</p>"><p>Here
    >is another paragraph</p></a>
    ></body>
    ></html>
    >>

    >Any advice would be greatly appreciated
  • No.3 | | 564 bytes | |

    Shaun wrote:

    >Hi M,
    >
    >Thanks for your help, the code works fine except if there is a line break in
    >the html, for example
    >this works
    >
    ><p>test</p>
    >
    >But this doesnt
    >
    ><p>test
    ></p>
    >
    >Any ideas?
    >


    See the last user contributed note from *csaba at alum dot mit dot edu
    *at ,
    particularly the last paragraph. He gives a very good explanation of
    pattern modifiers that answers your question.

    kgt

Re: Regex to wrap <a href=""> tag around a <p> tag


max 4000 letters.
Your nickname that display:
In order to stop the spam: 9 + 8 =
QUESTION ON "PHP"

EMSDN.COM