How can I code a line break in the search string of a str_replace function? My text editor is Notepad.
A.
Printable View
How can I code a line break in the search string of a str_replace function? My text editor is Notepad.
A.
Mmm, that's the PHP function str_replace you are talking about, right? If so, just follow the instructions on the PHP man page for that function (they work regardless of the editor you are using):
http://php.net/manual/en/function.str-replace.php
A line break may be introduced using (adding or substituting for something else) "\n". That will produce a carriage return/linefeed combo which creates a line break on all Unix/Apple/PC platforms.
Make sure to use "\n" - if you use '\n' - all you will get is a literal \n sequence. Double quotes in PHP interpret (whenever possible) the quoted characters, whereas single quotes almost always produce literal representations.
Oh, if the output is to be read and displayed as HTML, you would probably want to use '<br>' instead of "\n".
John, thanks. Unfortunately it does not work. Here is the coded line:The second double quote in the replace string is actually ampersand-number sign-8-2-2-0-semicolon. The DD editor converts it.PHP Code:str_replace("<br />\n<br />", "“<br />\n<br />", $testitable [$element + 2]);
A.
Searching on "\n" is not the same as replacing something with it. In order to match, the subject must contain CRLF at that point. Does it? Even if it does, all you are adding is:
“
is that your objective?
What does a typical input string look like?
John,
My objective is to add the open quote (ampersand-pound sign-8-2-2-0) to the beginning of subsequent text paragraphs. Most of the texts are only one paragraph and the initial open quote is trivial. It is only multiparagraph text that is at issue. What I am doing specifically, is this: There is an html table of testimonials on http://www.njexpungements.com/testimonials.php. I want to take a single random entry from that table and include it at the bottom of every other page on that site. Grammatically, text preceded by <br /><br /> must be preceded by an open quote. Here is the code that accomplishes that (except for the multiparagraph situation).A.PHP Code:<?php
if ( $here !== 'Testimonials' ) { // only one to a customer
$testistring = file_get_contents("testimonials.php"); // fetch the raw source page
$testistring = substr($testistring, 0, strpos($testistring, "</table>")); // drop tail
$testistring = str_replace("</td>", "", $testistring); // elim unsightly tags
$testistring = str_replace("<tr>", "", $testistring); // elim unsightly tags
$testistring = str_replace("</tr>", "", $testistring); // elim unsightly tags
$testitable = explode("<td>", $testistring); // create array
array_shift($testitable); // drop ado
$element = mt_rand (0, count($testitable)); // randomize element
$element = $element - ($element % 3); // force beginning of triplet
echo "Comment of former client" . $testitable [$element] . "(file " . $testitable [$element + 1] . "): “" . trim($testitable [$element + 2]) ."” <br /><br />[For comments of other former clients, see our <a href='testimonials.php'>testimonials </a> page.]<br />"; // display accolade.
}
?>
I had trouble getting the contents of the file into the array. But I'm assuming that's not an issue on your server (where conditions presumably are at least a little different). Once I solved that issue, I changed the line (all one line):
to (all one line):PHP Code:echo "Comment of former client" . $testitable [$element] . "(file " . $testitable [$element + 1] . "): “" . trim($testitable [$element + 2]) ."” <br /><br />[For comments of other former clients, see our <a href='testimonials.php'>testimonials </a> page.]<br />"; // display accolade.
And things appeared to work out well for me.PHP Code:echo "Comment of former client" . $testitable [$element] . "(file " . $testitable [$element + 1] . "): “" . trim(preg_replace('"<br />[\x0d\x0a]*<br />[\x0d\x0a]*"', "<br />\n<br />\n“", $testitable [$element + 2])) ."” <br /><br />[For comments of other former clients, see our <a href='testimonials.php'>testimonials </a> page.]<br />"; // display accolade.
Notes: Using str_replace requires literals as the only components of the search string. I did that, using actual line breaks and it worked, but looked very cumbersome, and wouldn't work if the editor that made the literal line breaks used a different method than the server (Mac or PC style line breaks vs Linux or Windows style, there might be other possibilities) Sometimes they match, sometimes not. So I elected to go with preg_replace which allows one to represent a line break as 0 or more of the characters hex 0d and hex 0a. This should satisfy any server's convention for a line break, and even cases where there simply are 2 br tags with no intervening line break. I also chose to include an additional line break and move the opening quote character to the end of that because it looked like what you were after (satisfies observed usage/quoting conventions I'm familiar with).
Any questions or problems, just let me know.
John,
Can you please check to see whether the DD editor translated some of what you typed, e.g. ampersand-pound sign-8-2-2-0-semicolon, to the character it represents? ALSO, did some of what you intended to code get chopped off at the end?
A.
Maybe chopped off (but only if you didn't copy all of it). I used the literal open quote, rather than the entity, and that worked. Here's what I used again, in generic code tags (PHP tags can be harder to copy and paste from) (again it's all one line and to make sure you get it all, it ends with "// display accolade. " so make sure you get it all):
Was this working for you before? I mean before my suggestion, was it giving you random testimonials?Code:echo "Comment of former client" . $testitable [$element] . "(file " . $testitable [$element + 1] . "): “" . trim(preg_replace('"<br />[\x0d\x0a]*<br />[\x0d\x0a]*"', "<br />\n<br />\n“", $testitable [$element + 2])) ."” <br /><br />[For comments of other former clients, see our <a href='testimonials.php'>testimonials </a> page.]<br />"; // display accolade.
Here's my complete working demo - should work on any PHP (v5.3+) server that can fetch remote files:
Attachment 5566
To answer your question, it was working before (except for the open quotation marks on paragraphs subsequent to the first one). You can see it "in action" at any of the pages on that site (except the Testimonials page, which it is programmed to bypass).
I plugged your working demo code into my test page and, voila, it works perfectly! I lack the time right now to plug it into my production file, but will do so in the near future. What I will also do, when I find the time, is review your code, token by token, for pedagogical purposes.
A.