I need some help with PHP please (preg_match_all)
Discussion
I have a custom function which adds a <br /> to text.
However, I want it to ignore more than just the single <table> tag I have here.
I have tried preg_match_all using "/<pre><code><table>(.*?)\</code></pre></table>/s" as my string to match with a foreach loop but I just can't seem to get it to work.
I just keep getting warnings: "PHP Notice: Array to string conversion"
Can anybody help please?
Below is my working code:
However, I want it to ignore more than just the single <table> tag I have here.
I have tried preg_match_all using "/<pre><code><table>(.*?)\</code></pre></table>/s" as my string to match with a foreach loop but I just can't seem to get it to work.
I just keep getting warnings: "PHP Notice: Array to string conversion"
Can anybody help please?
Below is my working code:
working code said:
function custom_nl2br($html) {
$pattern = "/<table>(.*?)<\/table>/s";
preg_match($pattern, $html, $matches);
$html = nl2br(str_replace($matches[0], '[placeholder]', $html));
$html = str_replace('[placeholder]',$matches[0], $html);
return $html;
}
$pattern = "/<table>(.*?)<\/table>/s";
preg_match($pattern, $html, $matches);
$html = nl2br(str_replace($matches[0], '[placeholder]', $html));
$html = str_replace('[placeholder]',$matches[0], $html);
return $html;
}
code I have tried said:
function custom_nl2br($html) {
$pattern = "/<pre><code><table>(.*?)\</code></pre></table>/s";
preg_match_all($pattern, $html, $matches);
foreach($matches[1] as $a) {
$html = str_replace($a, str_replace("<br />", '', $a), $html);
}
return $html;
}
$pattern = "/<pre><code><table>(.*?)\</code></pre></table>/s";
preg_match_all($pattern, $html, $matches);
foreach($matches[1] as $a) {
$html = str_replace($a, str_replace("<br />", '', $a), $html);
}
return $html;
}
It's largely plain text being read from the database, with some html content.
It looks like this sample:
What I'm trying to achieve, is to prevent a <br /> tag being appended to lines between html tags, and only appended to plain text.
I can do it with one set of tags (using the code above) but I want to look through the entire dataset and ensure nothing is added between tags.
I have tested my regex and it does seem to work - but I suspect it's the foreach loop that's not working properly.
It looks like this sample:
<h1>This is a test</h1>
This is some test text.
And a second line.
And a third line.
<pre>
This is some preformatted text.
With another line.
And another line.
</pre>
<table>
<tr><td>column</td></tr>
<tr><td>column2</td></tr>
<tr><td>columns3</td></tr>
</table>
What I'm trying to achieve, is to prevent a <br /> tag being appended to lines between html tags, and only appended to plain text.
I can do it with one set of tags (using the code above) but I want to look through the entire dataset and ensure nothing is added between tags.
I have tested my regex and it does seem to work - but I suspect it's the foreach loop that's not working properly.
mikef said:
So you're looking for <pre> OR <code> OR <table>, not embedded tags?
Wouldn't that be
?
Yes I think you could be right - although all the example I found on Stackoverflow etc. suggest that the regex should just contain the fields are I posted previously.Wouldn't that be
(<pre>|<code>|<table>)(.*?)(<\/table>|<\/code>|<\/pre>)
?
I shall try your suggestion, thanks.
TonyRPH said:
mikef said:
So you're looking for <pre> OR <code> OR <table>, not embedded tags?
Wouldn't that be
?
Yes I think you could be right - although all the example I found on Stackoverflow etc. suggest that the regex should just contain the fields are I posted previously.Wouldn't that be
(<pre>|<code>|<table>)(.*?)(<\/table>|<\/code>|<\/pre>)
?
I shall try your suggestion, thanks.
You'd be better off having three distinct regexes matching the pre, table and code tags and their respective end tags imo. I don't think there'd be that much of a performance hit unless you're regexing across loads of text.
Just to update this.
I got it around it with CSS instead.
I created a new class and wrapped the text I don't want any <br /> tags with this new class, that simply 'hides' the <br /> (using display: none) from the browser and hence is not rendered.
I also added "content: \a" and "white-space: pre" tags which inserts a line break without the <br />.
I got it around it with CSS instead.
I created a new class and wrapped the text I don't want any <br /> tags with this new class, that simply 'hides' the <br /> (using display: none) from the browser and hence is not rendered.
I also added "content: \a" and "white-space: pre" tags which inserts a line break without the <br />.
CSS said:
.scripts-bg {
background-color: gainsboro;
font-family: monospace;
padding: 10px;
content: "\a";
white-space: pre;
}
.scripts-bg br {
display: none;
}
background-color: gainsboro;
font-family: monospace;
padding: 10px;
content: "\a";
white-space: pre;
}
.scripts-bg br {
display: none;
}
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff


