Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post

    regex preg_match_all question

    I was working on a method of scraping my own file into another page:
    PHP Code:
    $contents_of_page file_get_contents('bible.html');

    function 
    getTextBetweenTags($string$tagname) {
        
    $pattern "/<$tagname ?.*>(.*)<\/$tagname>/";
        
    preg_match_all($pattern$string$matches);
        return 
    $matches[1];
    }

    $str '<table><tr><td>1</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>1</td><td>1</td><td>In the beginning God created the heaven and the earth.</td></tr></table>';
    $txt getTextBetweenTags($str"td");
    print_r($txt); 
    $txt brings only In the beginning God created the heaven and the earth.

    What I want is to replace:
    PHP Code:
    $txt '<table><tr><td>1</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>1</td><td>1</td><td>In the beginning God created the heaven and the earth.</td></tr></table>'
    into
    PHP Code:
    $txt '<table><tr><td class="book">1</td><td class="chapter">1</td><td class="verse">1</td><td class="recordType">gn</td><td class="book_title">Genesis</td><td class="book_spoke">1</td><td class="chapter_spoke">1</td><td class="verse_spoke">1</td><td class="something_else">1</td><td>In the beginning God created the heaven and the earth.</td></tr></table>'
    Compare bible texts (and other tools):
    TheWheelofGod

  • #2
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post
    I was working on it and got this so far:
    PHP Code:
    $string "<table><tr><td>1</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>1</td><td>1</td><td>In the beginning God created the heaven and the earth.</td></tr></table>";
    $patterns[0] = "/<td/";
    $names = Array("book""chapter""verse""recordType""book_title""book_spoke""chapter_spoke""verse_spoke""something_else""text_data");

    for(
    $repNames=0$repNames<count($names); $repNames++){
        
    $replacements[$repNames] = "<td class=\"".$names[$repNames]."\"";

    }
    echo 
    preg_replace($patterns$replacements$string)."\n"
    One thing I don't understand is why are all the classes book?
    Code:
    <table>
        <tr>
            <td class="book">1</td>
            <td class="book">1</td>
            <td class="book">1</td>
            <td class="book">gn</td>
            <td class="book">Genesis</td>
            <td class="book">1</td>
            <td class="book">1</td>
            <td class="book">1</td>
            <td class="book">1</td>
            <td class="book">In the beginning God created the heaven and the earth.</td>
        </tr>
    </table>
    There seems something wrong with the for loop. It's only reading the first in the array.
    Compare bible texts (and other tools):
    TheWheelofGod

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by gilgalbiblewhee View Post
    I was working on it and got this so far:
    PHP Code:
    $string "<table><tr><td>1</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>1</td><td>1</td><td>In the beginning God created the heaven and the earth.</td></tr></table>";
    $patterns[0] = "/<td/";
    $names = Array("book""chapter""verse""recordType""book_title""book_spoke""chapter_spoke""verse_spoke""something_else""text_data");

    for(
    $repNames=0$repNames<count($names); $repNames++){
        
    $replacements[$repNames] = "<td class=\"".$names[$repNames]."\"";

    }
    echo 
    preg_replace($patterns$replacements$string)."\n"
    One thing I don't understand is why are all the classes book?
    Code:
    <table>
        <tr>
            <td class="book">1</td>
            <td class="book">1</td>
            <td class="book">1</td>
            <td class="book">gn</td>
            <td class="book">Genesis</td>
            <td class="book">1</td>
            <td class="book">1</td>
            <td class="book">1</td>
            <td class="book">1</td>
            <td class="book">In the beginning God created the heaven and the earth.</td>
        </tr>
    </table>
    There seems something wrong with the for loop. It's only reading the first in the array.
    replace this:
    PHP Code:
    for($repNames=0$repNames<count($names); $repNames++){
        
    $replacements[$repNames] = "<td class=\"".$names[$repNames]."\"";


    with this:
    PHP Code:
    foreach($names as $repNames => $name){
            
    $patterns[] = $patterns[0];
            
    $replacements[] = '<td class="'.$name.'"';

    the $patterns and $replacements arrays must have the same size. The rest, using foreach instead of for and single quote instead of double are just to make the code easy to read.

    best regards

  • #4
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by oesxyl View Post
    replace this:
    PHP Code:
    for($repNames=0$repNames<count($names); $repNames++){
        
    $replacements[$repNames] = "<td class=\"".$names[$repNames]."\"";


    with this:
    PHP Code:
    foreach($names as $repNames => $name){
            
    $patterns[] = $patterns[0];
            
    $replacements[] = '<td class="'.$name.'"';

    the $patterns and $replacements arrays must have the same size. The rest, using foreach instead of for and single quote instead of double are just to make the code easy to read.

    best regards
    PHP Code:
    $string "<table><tr><td>1</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>1</td><td>1</td><td>In the beginning God created the heaven and the earth.</td></tr></table>";
    $patterns[0] = "/<td/";
    $names = Array("book""chapter""verse""recordType""book_title""book_spoke""chapter_spoke""verse_spoke""something_else""text_data");

    foreach(
    $names as $repNames => $name){
            
    $patterns[] = $patterns[0];
            
    $replacements[] = '<td class="'.$name.'"';

    echo 
    preg_replace($patterns$replacements[$repNames], $string)."\n"
    I got this result:
    Code:
    <table>
    <tr>
    <td class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data">1</td>
    
    <td class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data">1</td>
    
    <td class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data">1</td>
    
    <td class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data">gn</td>
    
    <td class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data">Genesis</td>
    
    <td class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data">1</td>
    
    <td class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data">1</td>
    
    <td class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data">1</td>
    
    <td class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data">1</td>
    
    <td class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data" class="text_data">In the beginning God created the heaven and the earth.</td></tr>
    </table>
    Compare bible texts (and other tools):
    TheWheelofGod

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post
    I'll answer my own problem:
    PHP Code:
    $string "<table><tr><td>1</td><td>1</td><td>1</td><td>gn</td><td>Genesis</td><td>1</td><td>1</td><td>1</td><td>1</td><td>In the beginning God created the heaven and the earth.</td></tr></table>";
    $patterns[0] = "/<td>/";
    $names = Array("book""chapter""verse""recordType""book_title""book_spoke""chapter_spoke""verse_spoke""something_else""text_data");

    foreach(
    $names as $repNames => $name){
            
    $patterns[] = $patterns[0];
            
    $replacements[] = "<td class=\"".$name."\">";

    echo 
    preg_replace($patterns$replacements$string1)."\n"
    echo preg_replace($patterns, $replacements, $string, 1)."\n";
    Code:
    <table>
    <tr>
    <td class="book">1</td>
    <td class="chapter">1</td>
    <td class="verse">1</td>
    <td class="recordType">gn</td>
    <td class="book_title">Genesis</td>
    <td class="book_spoke">1</td>
    <td class="chapter_spoke">1</td>
    <td class="verse_spoke">1</td>
    <td class="something_else">1</td>
    <td class="text_data">In the beginning God created the heaven and the earth.</td>
    </tr>
    </table>
    Compare bible texts (and other tools):
    TheWheelofGod


  •  

    Posting Permissions

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