Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: To Code a Line Break

  1. #1
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    224
    Thanks
    80
    Thanked 3 Times in 3 Posts

    Default To Code a Line Break

    How can I code a line break in the search string of a str_replace function? My text editor is Notepad.

    A.
    Last edited by marain; 12-21-2014 at 05:13 PM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    224
    Thanks
    80
    Thanked 3 Times in 3 Posts

    Default

    John, thanks. Unfortunately it does not work. Here is the coded line:
    PHP Code:
    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.
    Last edited by marain; 12-16-2014 at 11:04 AM. Reason: Explaining that the graphic displayed is a translation from what I uploaded.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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?
    Last edited by jscheuer1; 12-17-2014 at 01:02 AM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    224
    Thanks
    80
    Thanked 3 Times in 3 Posts

    Default

    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 Code:
    <?php
    if ( $here !== 'Testimonials' ) {                // only one to a customer
        
    $testistring file_get_contents("testimonials.php");    // fetch the raw source page
        
    $testistring substr($testistring0strpos($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 (0count($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]) ."”&nbsp;&nbsp;<br /><br />[For comments of other former clients, see our <a href='testimonials.php'>testimonials </a> page.]<br />";    // display accolade.
    }
    ?>
    A.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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):

    PHP Code:
     echo "Comment of former client" $testitable [$element] . "(file " $testitable [$element 1] . "):  " trim($testitable [$element 2]) ."&nbsp;&nbsp;<br /><br />[For comments of other former clients, see our <a href='testimonials.php'>testimonials </a> page.]<br />";    // display accolade. 
    to (all one line):

    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])) ."&nbsp;&nbsp;<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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    224
    Thanks
    80
    Thanked 3 Times in 3 Posts

    Default

    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.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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):

    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])) ."&nbsp;&nbsp;<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?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Here's my complete working demo - should work on any PHP (v5.3+) server that can fetch remote files:

    testimonial-h.zip
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. The Following User Says Thank You to jscheuer1 For This Useful Post:

    marain (12-18-2014)

  11. #10
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    224
    Thanks
    80
    Thanked 3 Times in 3 Posts

    Default

    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.

Similar Threads

  1. GD Image - Line Break?
    By Vernier in forum PHP
    Replies: 7
    Last Post: 08-16-2012, 02:53 PM
  2. CSS Extra Line Break
    By Johnnymushio in forum CSS
    Replies: 2
    Last Post: 04-07-2011, 06:15 PM
  3. line break in mySql
    By ggalan in forum MySQL and other databases
    Replies: 4
    Last Post: 07-29-2010, 05:25 AM
  4. line break before pre tag, get rid of
    By chopficaro in forum CSS
    Replies: 4
    Last Post: 03-16-2010, 01:31 AM
  5. IE adds line break?
    By thenajsays in forum CSS
    Replies: 4
    Last Post: 06-10-2008, 04:13 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •