Code: Select all
$new_string=str_replace('<p>', '<p class="whatever">', preg_match('|<p class="whatever">(.+)<p class="whatever">|', $original_string));Any ideas?
Thanks,
Drew
Moderator: General Moderators
Code: Select all
$new_string=str_replace('<p>', '<p class="whatever">', preg_match('|<p class="whatever">(.+)<p class="whatever">|', $original_string));Code: Select all
<?php
// Makes debugging easier (no need for htmlentities())
header('Content-Type: text/plain');
// The source document
$html = <<<HTML
<html>
<head>
<title>Sample</title>
</head>
<body>
<div class="whatever"></div>
<div class="nonewline"><p class="sameline"><div>div in p in div</div></p></div>
<div class="whatever">
<p class="whatever">Gets this (1)
<div>Div 1</div>
<p>embedded parapraphs break the pattern; stops at this end tag</p>
<div>Div 2</div>
</p>
<p>Do not get</p>
<p class="whatever">Gets this (2)</p>
</div>
<p class="whatever"></p><!-- Gets the previous paragraph (3) -->
<p>Do not get this either</p>
<p class="whatever">
<div>Gets this (4)</div>
</p>
</body>
</html>
HTML;
/*
* # = pattern delimiter
* (<p class="whatever">) = subpattern to match opening tag
* ((\v*.*)*) = subpattern to match between tags
* \v = vertical whitespace (newline) (Since PHP 5.2.4)
* . = any character except newline
* * = match zero or more
* (</p>) = subpattern to match closing tag
* U = ungreedy
*/
$pattern = '#(<p class="whatever">)((\v*.*)*)(</p>)#U';
// Finds all matches, stores them in $matches
preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
// Equivalent to <hr /> for plain text
$separator = "\n" . str_repeat('-', 80) . "\n";
// Displays the results for each match
foreach ($matches as $m)
{
echo $separator . "\n"
. 'WHOLE MATCH: ' . $m[0] . "\n"
. 'OPENING TAG: ' . $m[1] . "\n"
. 'PARAGRAPH : ' . $m[2] . "\n"
. 'LAST LETTER: ' . $m[3] . "\n"
. 'CLOSING TAG: ' . $m[4] . "\n"
;
}
echo $separator . "\n";
?>I need to get a better idea of what you are trying to match and what you are trying to replace it with.oboedrew wrote:... locate a section that begins and ends with paragraphs of class="whatever," and turn all the paragraphs between those two into class="whatever" also. ...
Code: Select all
<p class="whatever">
<p></p>
<p></p>
<p></p>
</p>Code: Select all
<p class="whatever">
<p class="whatever"></p>
<p class="whatever"></p>
<p class="whatever"></p>
</p>Code: Select all
<p class="whatever"></p>
<p></p>
<p></p>
<p></p>
<p class="whatever"></p>Code: Select all
<p class="whatever"></p>
<p class="whatever"></p>
<p class="whatever"></p>
<p class="whatever"></p>
<p class="whatever"></p>Code: Select all
<?php
$t = file_get_contents('example.txt');
$t = str_replace("\r", '', $t);
$t = htmlentities($t);
$t = "<p>$t</p>";
$t = str_replace("\n\n", '</p><p>', $t);
$t = str_replace("\n", '<br />', $t);
$t = preg_replace('#\[title\](.*?)\[/title\]#i', '<p class="title">\1</p>', $t);
$t = preg_replace('#\[subtitle\](.*?)\[/subtitle\]#i', '<p class="subtitle">\1</p>', $t);
$t = preg_replace('#\[i\](.*?)\[/i\]#i', '<i>\1</i>', $t);
$t = preg_replace('#\[b\](.*?)\[/b\]#i', '<b>\1</b>', $t);
$t = preg_replace('#\[quote\](.*?)\[/quote\]#i', '<p class="quote">\1</p>', $t);
$t = preg_replace('#\[url\](.*?)\[/url\]#i', '<a href="\1">\1</a>', $t);
$t = preg_replace('#\[url=(.*?)\](.*?)\[/url\]#i', '<a href="\1">\2</a>', $t);
$t = str_replace('</p><br />', '</p><p>', $t);
$t = str_replace('<p><p', '<p', $t);
$t = str_replace('</p></p>', '</p>', $t);
$t = str_replace('</p><p', "</p>\n<p", $t);
echo $t;
?>Code: Select all
header('Content-Type: text/plain');Code: Select all
echo $t . "\n\n-----\n\n";Code: Select all
while(preg_match('#<p>\[quote](.+)\[/quote]</p>#s', $entry, $match)){
$match[1]=str_replace('<p>', "\t<p>", $match[1]);
$entry=str_replace($match[0], "<div class=\"quote\">\n\t<p>$match[1]</p>\n</div>", $entry);
}
Code: Select all
preg_match_all('#<p>\[quote](.+)\[/quote]</p>#s', $entry, $matches);
foreach($matches as $match){
$match[1]=str_replace('<p>', "\t<p>", $match[1]);
$entry=str_replace($match[0], "<div class=\"quote\">\n\t<p>$match[1]</p>\n</div>", $entry);
}
It is not necessary. I just like to escape special characters even if they will not be treated as special characters in the current context (in case the pattern grows).oboedrew wrote:why escape the closing square bracket too?
The question mark prevents the .* pattern from being greedy and consuming the [/bbtag].oboedrew wrote:what is the meaning of the ? in (.*?)
If you don't want to match empty BBCode tags, then use the plus sign. If there are empty BBCode tags, they will be shown literally.oboedrew wrote:why allow for 0 or more characters (.*) instead of one or more (.+) when BBCode tags should never be left empty?
I'll get back to you on that.oboedrew wrote:I've also come up with an alternative solution...
Code: Select all
preg_match_all('#<p>\[quote](.+?)\[/quote]</p>#s', $entry, $matches, PREG_SET_ORDER);
foreach($matches as $match){
$match[1]=str_replace('<p>', "\t<p>", $match[1]);
$entry=str_replace($match[0], "<div class=\"quote\">\n<p>$match[1]</p>\n</div>", $entry);
}