You are here: Re: [PHP] Regex to wrap <a href=""> tag around a <p> tag « PHP « IT news, forums, messages
Re: [PHP] Regex to wrap <a href=""> tag around a <p> tag

Posted by M on 10/20/16 11:33

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 <p> tag and output it to the browser. This is how
> far I have got:
>
> // Get file contents
> $file_contents = file_get_contents($file);
> // Replace <p> tags
> $file_contents = preg_replace('/^<p\>[a-z][0-9]<\/p>$/', '/^<a
> href="edit_paragraph&text="">\<p>[a-z][0-9]<\/p><\/a>$/', $file_contents);
> // Output to browser
> echo $file_contents;
>
> I have two problems.
>
> 1. - The regex doesn't work!
> 2. - I need to add the <p> tags 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
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация