View Full Version : Resolved To Code a Line Break
marain
12-16-2014, 03:04 AM
How can I code a line break in the search string of a str_replace function? My text editor is Notepad.
A.
jscheuer1
12-16-2014, 05:42 AM
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".
marain
12-16-2014, 11:01 AM
John, thanks. Unfortunately it does not work. Here is the coded line:
str_replace("<br />\n<br />", "“<br />\n<br />", $testitable [$element + 2]);
The second double quote in the replace string is actually ampersand-number sign-8-2-2-0-semicolon. The DD editor converts it.
A.
jscheuer1
12-17-2014, 12:54 AM
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?
marain
12-17-2014, 02:01 PM
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).
<?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.
}
?>
A.
jscheuer1
12-17-2014, 06:15 PM
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):
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.
to (all one line):
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.
And things appeared to work out well for me.
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.
marain
12-17-2014, 10:48 PM
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.
jscheuer1
12-18-2014, 12:13 AM
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):
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.
Was this working for you before? I mean before my suggestion, was it giving you random testimonials?
jscheuer1
12-18-2014, 12:21 AM
Here's my complete working demo - should work on any PHP (v5.3+) server that can fetch remote files:
5566
marain
12-18-2014, 01:18 AM
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.
jscheuer1
12-18-2014, 02:00 AM
Great! As I said before - any questions/problems, just let me know. Oh, and it's always possible there's a simpler or more efficient method. But I feel confident this is at the very least serviceable.
marain
12-20-2014, 11:27 PM
John, the original problem arose on account of my inability to communicate to my text editor (Notepad) that I wanted to search for hard returns. When I entered a hard return that I wanted interpreted as part of a search string character, the text editor interpreted it instead as a hard return end of line. You solved that problem by suggesting that, in place of hard returns, I should use hexadecimal ascii replacement characters, i.e. "\x0d\x0a\x0d\x0a".
Your solution made my php code do exactly what I wanted it to do. Part of your solution, however, was to replace str_replace with preg_replace. On reviewing your solution, I speculated that, using your hexadecimal ascii substitutions, I ought to be able to implement your solution more simply by using str_replace. I attempted to do that. More particularly, in place of
trim(preg_replace('"<br />[\x0d\x0a]*<br />[\x0d\x0a]*"', "<br />\n<br />\n“", $testitable [$element + 2])); I used
trim(str_replace("\x0d\x0a\x0d\x0a", "\x0d\x0a\x0d\x0a“", $testitable [$element + 2]));
My substitution yields strange results. More particularly, what it causes is to not begin text following two hard returns with two hard returns followed by “ (open quote). Rather, what it causes is to append two “'s at the end of $testitable [$element + 2], regardless of whether that element contains zero, one, or more than one double hard return. You can see those incorrect results at http://www.njexpungements.com/test.php .
I believe I'm getting closer in my quest for simplification, but I'm obviously not there yet. So my question is, must I use preg_replace, or can I accomplish the same thing with str_replace? In the meanwhile, as mentioned above, your suggested code is (to use your term) serviceable, for which you have my continuing thanks.
A.
marain
12-21-2014, 04:27 PM
Got it!
echo "Comment of former client" . $testitable [$element] . "(file " . $testitable [$element + 1] . "): “" . trim(str_replace("<br />\x0d\x0a<br />", "<br />\x0d\x0a<br />“", $testitable [$element + 2])) . "”<br /><br />[For comments of other former clients, see our <a href='testimonials.php'>testimonials </a> page.]<br /><br />";
Thank you!
A.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.