Results 1 to 8 of 8

Thread: PHP perl and pattern matching

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,182
    Thanks
    96
    Thanked 99 Times in 97 Posts

    Default PHP perl and pattern matching

    I'm kinda new to PHP's pattern matching function.

    here is the working code:

    PHP Code:
    $two="/\r\n\r\n/"
    $summary=htmlentities($summary); 
    $summary=preg_replace("$two","Ý",$summary);
    $summary=preg_replace("/Ý+/","<br><br>",$summary);
    $summary=html_entity_decode($summary);
    $summary=preg_replace("/<p>/","",$summary); 
    What I want to do is something like this

    PHP Code:
    $summary=preg_replace("/\r\n\r\n+/","<br><br>",$summary); 
    where the '+' is applied to \r\n\r\n and not just \n. In the first example I converted it to a weird character like &#221; and applied the '+' to that instead and then reconverted the chained occurances of &#221; to a single occurance of <br><br>.

    For visitors who are a bit lost here and is a short explanation of some of the terms:

    \r = a carriage return. A carriage return is what happens when you want to go down a line and to the left using an old fashioned type writer.
    \n = newline. it goes directly down a line.
    When typing a web document the enter button creates two non-printing characters /r/n that both occur together for some reason.
    preg_replace replaces all occurances of a term from a string.
    html_entity_decode and htmlentities are just ways to convert a web file into a different format like making < appear as &lt; instead.
    Last edited by james438; 06-01-2007 at 10:01 AM.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,182
    Thanks
    96
    Thanked 99 Times in 97 Posts

    Default

    any ideas?

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I think str_replace("\r\n\r\n", '<br>', $summary) would do as well... or maybe I'm misunderstanding you.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,182
    Thanks
    96
    Thanked 99 Times in 97 Posts

    Default

    Well, I am trying to get the '+' to be applied to all of /r/n/r/n.

    No worries, this isn't really very important. I found a workaround, but I figure there must be a way to do it.

    Thanks for looking into it.

  5. #5
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    When using the function str_replace() is searches all global occurences anyways. It doesn't take a regular expression as a parameter.
    - Mike

  6. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,182
    Thanks
    96
    Thanked 99 Times in 97 Posts

    Default

    but what if I wanted one

    what if I wanted it to recognize chained occurances of carriage returns, but only if the chain is 2 occurances or more or between 2 and 5 long, but if it is more than 5 then it will be ignored?

    I have been teaching myself from these two sites here and here. In particular this link shows how to recognize chained occurances of something, but I have not figured out how to get it to recognize chained occurances of /r/n/r/n as opposed to just /s, which is pretty easy, because i just add a + to make it /s+ so that it will recognize chained white space.

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    '/((:?\r\n){2, 5})/g'
    will match "\r\n" repeated between two and five times.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,182
    Thanks
    96
    Thanked 99 Times in 97 Posts

    Default

    thanks,

    it was kind of a silly thing, but I was curious

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
  •