Regex to wrap <a href=""> tag around a <p> tag
| From: | Shaun | Date: | Wed, 30 Nov 2005 23:41:21 +0000 |
| Subject: | Regex to wrap <a href=""> tag around a <p> tag | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-226672@lists.php.net to get a copy of this message | ||
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