breaking line with regex(regular Expression or any method)

Discussion in 'PHP' started by ksamir2004, Apr 23, 2008.

  1. #1
    Hi All,

    Can any one help me to breaking String using regular expression or any other string method.

    $Str= "1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem"


    Ans should be:

    1. hello this is php page.
    2. I believe PHP is the BEST section for this topic
    3. hello world please solve this problem


    Thanks
    Sam
     
    ksamir2004, Apr 23, 2008 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    
    preg_match_all('/[0-9]+\.[a-zA-Z \.]+/U',$stringtosearchin,$result);
    print_r($result);
    
    PHP:
    Something like that should work (untested).

    Edit:
    This won't work if there are numbers in the title, if so, it has to be done manually.

    Peace,
     
    Barti1987, Apr 23, 2008 IP
  3. ksamir2004

    ksamir2004 Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    the output of above function is
    Array ( [0] => Array ( [0] => 1. [1] => 2. [2] => 3. ) )
     
    ksamir2004, Apr 23, 2008 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    Sorry:

    
    preg_match_all('/[0-9]+\.([a-zA-Z \.])+/U',$stringtosearchin,$result);
    print_r($result);
    //$result[1] should contain the info.
    
    PHP:
    Peace,
     
    Barti1987, Apr 23, 2008 IP
  5. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #5
    $Str = "1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem";
    
    echo sprintf('<pre>%s</pre>', preg_replace('#(\d+\. +)#', '$1'."\n", $Str));
    Code (markup):
     
    joebert, Apr 23, 2008 IP
  6. ksamir2004

    ksamir2004 Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi All,

    I need some more help on this script. The output of above function is this

    1.
    hello this is php page. 2.
    I believe PHP is the BEST section for this topic 3.
    hello world please solve this problem




    i need output like this
    1. hello this is php page.
    2. I believe PHP is the BEST section for this topic
    3. hello world please solve this problem

    can you help me for writing this script..

    Regard,
    Sam
     
    ksamir2004, Apr 23, 2008 IP
  7. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #7
    Try this.

    
    <?php
    $Str= "1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem";
    $match = preg_split('/([0-9]+)\./i', $Str);
    $ar = array();
    foreach ($match as $el) {
        $el = trim($el);
        if ($el != '') {
            $ar[] = $el;
        }
    }
    unset($match);
    echo('<pre>' . print_r($ar, true) . '</pre>');
    ?>
    
    <!-- Now with OL -->
    <ol>
        <?php foreach ($ar as $s) : ?>
        <li><?php echo($s) ?></li>
        <?php endforeach ; ?>
    </ol>
    
    PHP:
     
    xrvel, Apr 23, 2008 IP
  8. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #8
    Sorry about that, I put the newline on the wrong side.

    $Str = "1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem";
    
    echo sprintf('<pre>%s</pre>', preg_replace('#(\d+\. +)#', "\n".'$1', $Str));
    Code (markup):
    You may or may not want to trim the result if the first newline is a problem for you..
    $Str = "1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem";
    
    echo sprintf('<pre>%s</pre>', trim(preg_replace('#(\d+\. +)#', "\n".'$1', $Str)));
    Code (markup):
     
    joebert, Apr 23, 2008 IP
  9. ksamir2004

    ksamir2004 Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thank you very much..


    $Str= "1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem";
    $match = preg_split('/([0-9]+)\./i', $Str);
    unset($match[0]);
    foreach ($match as $s) :
    $i=(trim($s));
    echo '<li>'.$i .'</li>';
    endforeach ; ?>

    100% running this code..
    Thanks a lot..

    Regards,
    Sam
     
    ksamir2004, Apr 23, 2008 IP
  10. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #10
    I've updated the code :) That's the old one (there is a bug in the old one i think).
    You can check my post.

     
    xrvel, Apr 24, 2008 IP
  11. ksamir2004

    ksamir2004 Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hi

    i think there is no use of this function.


    $ar = array();foreach ($match as $el) { $el = trim($el); if ($el != '') { $ar[] = $el; }}
    echo('<pre>' . print_r($ar, true) . '</pre>');


    I am sending new updated solution.. sorry for intrupting..

    it will display output like this
     
    ksamir2004, Apr 24, 2008 IP
  12. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #12
    If you use my old code, and the $Str looks like this : (note that the 4th has nothing).
    $Str= "1. hello this is php page. 2. I believe PHP is the BEST section for this topic 3. hello world please solve this problem 4. 5. a";
    
    PHP:
    You'll get a display "bug"
    
    <ol>
            <li> hello this is php page. </li>
            <li> I believe PHP is the BEST section for this topic </li>
            <li> hello world please solve this problem </li>
            <li> </li> <!-- HERE is the empty -->
            <li> a</li>
    </ol>
    PHP:
    With the new code, empty element(s) will be removed
    
    <ol>
            <li> hello this is php page. </li>
            <li> I believe PHP is the BEST section for this topic </li>
            <li> hello world please solve this problem </li>
            <li> a</li>
    </ol>
    PHP:
     
    xrvel, Apr 24, 2008 IP
  13. ksamir2004

    ksamir2004 Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Thanx

    i modified your code.. This is good...
     
    ksamir2004, Apr 24, 2008 IP