Results 1 to 16 of 16

Thread: PHP help :)

  1. #1
    Join Date
    Jan 2008
    Location
    St. John's, NL
    Posts
    2,114

    PHP help :)

    Hi guys,

    I need some PHP help myself. I am pulling two fields from a MySQL table, keywords and content. Keywords is a space separated list of words to find in the content and replace with HTML anchors.

    Basically, my page has two columns. One is a list of the keywords (with links to each occurance underneath the word. The 2nd column is the content with keywords being linked to (HTML anchors).

    I have mostly working code below, but it seems to go into an infinite loop when a keyword occurs more than once in the content.

    For example, assume:

    keywords = test1 test2 test3
    content = content1 content2 content3 test1 test2 test3 test1 test2 test3

    This causes the script to infinite loop it seems.

    While:

    keywords = test1 test2 test3
    content = content1 content2 content3 test1 test2 test3

    Works fine.

    Please help me! Full code is below. I know the words list on the left is not done yet...

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    include(
    'inc/conn.php');
    $id mysql_real_escape_string($_GET['id']);

    $sql "SELECT * FROM transcripts WHERE id='$id' LIMIT 1";
    $result mysql_query($sql);
    $words mysql_result($result,0,'keywords');
    $content mysql_result($result,0,'content');
    // three arrays
    // one containing each key word
    // one containing the link name generated based on keyword and line number, one space between each link

    $keywords = array();
    $resultwords = array();

    $keywords preg_split("/[^\w]+/"$words);

        foreach (
    $keywords as $kword){
            
    // loop through each keyword
            
    $temp=$content;
            
    $datax=$content;
            
    $count 0;

                
    // find all occurences of keyword in content and replace with link
                // Also add link to word-specific array (as HTML)
                
    while (preg_match("/$kword/",$temp,$matches)){
                    
    $datax=str_replace($matches[$count],"<a name=\"$kword-".$count."\"></a>$kword",$temp);
                    
    // add link to array entry for this word, so we can display a list later
                    
    $resultwords[$kword] .= "<a href=\"$kword-".$count."\">$count</a> ";
                    
    $count++;
                }
            
            
    $content $datax;
        }

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Transcript Repository</title>
    <style type="text/css">
    p {
        margin:0px;
        padding:0px;
        padding-bottom:10px;
    }
    body {
        background-color:#FFFFFF;
        margin:0px;
        padding:0px;
        font-family:Geneva, Arial, Helvetica, sans-serif;
        font-size:12px;
        color:#000000;
    }
    div {
        margin:0px;
        padding:0px;
    }
    .leftdiv {
        float:left;
        width:20%;
    }
    .rightdiv {
        float:right;
        width:80%;
    }
    pre {
        margin:0px;
        padding:0px;
        font-family:Geneva, Arial, Helvetica, sans-serif;
        font-size:12px;
        color:#000000;
    }
    </style>
    </head>
    <body>
    <div class="leftdiv">
      <p style="font-weight:bold;">Keywords:</p>
      <p><?php echo $words?></p>
    </div>
    <div class="rightdiv">
      <pre>
    <?php echo $content?>
    </pre>
    </div>
    </body>
    </html>

  2. #2
    Join Date
    Jan 2008
    Location
    St. John's, NL
    Posts
    2,114
    Correction: it seems to go into an infinite loop regardless.

  3. #3
    Join Date
    Aug 2006
    Location
    Costa Blanca, Spain
    Posts
    144
    You are counting the times the while has run, but you havent told it to stop running when it has run a certain amount of times.
    James Bone, citruszebra
    citruszebra.cz // 03333 404 100 // cz-at-citruszebra.cz

    Any views are my own and not of any company I may represent.

  4. #4
    Join Date
    Jan 2008
    Location
    St. John's, NL
    Posts
    2,114
    The $count variable is merely meant to be a way of generating a new link for each occurance of a keyword. Example: keyword = test. Link name for the first would be "test0", 2nd would be "test1", and so forth.

  5. #5
    Join Date
    Aug 2006
    Location
    Costa Blanca, Spain
    Posts
    144
    //note to self, must read code properly!
    James Bone, citruszebra
    citruszebra.cz // 03333 404 100 // cz-at-citruszebra.cz

    Any views are my own and not of any company I may represent.

  6. #6
    Join Date
    Jan 2008
    Location
    St. John's, NL
    Posts
    2,114
    Nobody can fault you for trying to help

  7. #7
    Join Date
    Jan 2008
    Location
    St. John's, NL
    Posts
    2,114
    Anyone.. please?

  8. #8
    Join Date
    Apr 2003
    Location
    Los Angeles, CA
    Posts
    800
    Your input ($kword) to preg_match never changes, so you will always keep matching the same keyword, and the while loop will never break.
    Pings <1 ms, Unlimited Transfer, Lowest Price: http://localhost/

  9. #9
    Join Date
    Jan 2008
    Location
    St. John's, NL
    Posts
    2,114
    Still can't get it to work properly.. grrrr

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    include(
    'inc/conn.php');
    $id mysql_real_escape_string($_GET['id']);

    $sql "SELECT * FROM transcripts WHERE id='$id' LIMIT 1";
    $result mysql_query($sql);
    $words mysql_result($result,0,'keywords');
    $content mysql_result($result,0,'content');

    // somearrays
    // one containing each key word
    // one containing the link name generated based on keyword and line number, one space between each link

    $keywords = array();
    $resultwords = array();

    $keywords preg_split("/[^\w]+/"$words);
    $count 0;

        foreach (
    $keywords as $key => $value){
            
    // looping through each keyword
            
    $kword $value;
            
            
    preg_match("/[^>]$kword/",$content,$matches);
            
                
    // find all occurences of keyword in content and replace with link
                // Also add link to word-specific array (as HTML)
                
    foreach ($matches as $key => $value){
                    
    $content=str_replace($value,"<a name=\"$value-".$count."\"></a>$value",$content);
                    
    // add link to array entry for this word, so we can display a list later
                    
    $resultwords[$kword] .= "<a href=\"$value-".$count."\">$count</a> ";
                    
    $count++;
                }
            
        }

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Transcript Repository</title>
    <style type="text/css">
    p {
        margin:0px;
        padding:0px;
        padding-bottom:10px;
    }
    body {
        background-color:#FFFFFF;
        margin:0px;
        padding:0px;
        font-family:Geneva, Arial, Helvetica, sans-serif;
        font-size:12px;
        color:#000000;
    }
    div {
        margin:0px;
        padding:0px;
    }
    .leftdiv {
        float:left;
        width:20%;
    }
    .rightdiv {
        float:right;
        width:80%;
    }
    pre {
        margin:0px;
        padding:0px;
        font-family:Geneva, Arial, Helvetica, sans-serif;
        font-size:12px;
        color:#000000;
    }
    </style>
    </head>
    <body>
    <div class="leftdiv">
      <p style="font-weight:bold;">Keywords:</p>
      <p><?php echo $words?></p>
    </div>
    <div class="rightdiv">
      <pre>
    <?php echo $content?>
    </pre>
    </div>
    </body>
    </html>

  10. #10
    Join Date
    Jan 2006
    Location
    Athens, Greece
    Posts
    1,479
    Can you put some test content?

  11. #11
    Join Date
    Jan 2008
    Location
    St. John's, NL
    Posts
    2,114
    One better.. I'll put a slightly edited version (removing only MySQL stuff) so it can run for anyone who can try it.

    PHP Code:
    <?php
    error_reporting
    (E_ALL);

    $words "test1 test2 test3";
    $content "content1 content2 content3 test1 test2 test3 test4";

    // some arrays
    // one containing each key word
    // one containing the link name generated based on keyword and line number, one space between each link

    $keywords = array();
    $resultwords = array();

    $keywords preg_split("/[^\w]+/"$words);
    $count 0;

        foreach (
    $keywords as $key => $value){
            
    // looping through each keyword
            
    $kword $value;
            
            
    preg_match("/[^>]$kword/",$content,$matches);
            
                
    // find all occurences of keyword in content and replace with link
                // Also add link to word-specific array (as HTML)
                
    foreach ($matches as $key => $value){
                    
    $content=str_replace($value,"<a name=\"$value-".$count."\"></a>$value",$content);
                    
    // add link to array entry for this word, so we can display a list later
                    
    $resultwords[$kword] .= "<a href=\"$value-".$count."\">$count</a> ";
                    
    $count++;
                }
            
        }

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Transcript Repository</title>
    <style type="text/css">
    p {
        margin:0px;
        padding:0px;
        padding-bottom:10px;
    }
    body {
        background-color:#FFFFFF;
        margin:0px;
        padding:0px;
        font-family:Geneva, Arial, Helvetica, sans-serif;
        font-size:12px;
        color:#000000;
    }
    div {
        margin:0px;
        padding:0px;
    }
    .leftdiv {
        float:left;
        width:20%;
    }
    .rightdiv {
        float:right;
        width:80%;
    }
    pre {
        margin:0px;
        padding:0px;
        font-family:Geneva, Arial, Helvetica, sans-serif;
        font-size:12px;
        color:#000000;
    }
    </style>
    </head>
    <body>
    <div class="leftdiv">
      <p style="font-weight:bold;">Keywords:</p>
      <p><?php echo $words?></p>
    </div>
    <div class="rightdiv">
      <pre>
    <?php echo $content?>
    </pre>
    </div>
    </body>
    </html>

  12. #12
    Join Date
    Jan 2006
    Location
    Athens, Greece
    Posts
    1,479
    It doesn't work for me.
    Try a simpler version
    PHP Code:
    $words 'test1 test2 test3';
    $content 'content1 content2 content3 test1 test2 test3 test4 test2';
    //
    $words explode(' '$words);
    foreach (
    $words as $w)
    {
        
    $content str_replace($w'<a name="'.$w.'">'.$w.'</a>'$content);
    }
    echo 
    $content

  13. #13
    Join Date
    Jan 2008
    Location
    St. John's, NL
    Posts
    2,114
    Not bad.. a bit too simple for me though, as it lacks the ability to generate a unique <a name> for each time a word occurs.

  14. #14
    Join Date
    Aug 2006
    Location
    Costa Blanca, Spain
    Posts
    144
    PHP Code:
    $words 'test1 test2 test3';
    $content 'content1 content2 content3 test1 test2 test3 test4 test2';
    //
    $words explode(' '$words);
    $count 0;
    foreach (
    $words as $w)
    {
        
    $content str_replace($w'<a name="'.$w.'">'.$w.'</a>'$content);
        
    $count++;
    }
    echo 
    $content
    Then you can use $count as you did before
    James Bone, citruszebra
    citruszebra.cz // 03333 404 100 // cz-at-citruszebra.cz

    Any views are my own and not of any company I may represent.

  15. #15
    Join Date
    Jan 2008
    Location
    St. John's, NL
    Posts
    2,114
    I was able to get a working solution thanks to you bro

    PHP Code:
    $id mysql_real_escape_string($_GET['id']);

    $sql "SELECT * FROM transcripts WHERE id='$id' LIMIT 1";
    $result mysql_query($sql);
    $keywords mysql_result($result,0,'keywords');
    $content mysql_result($result,0,'content');

    // add extra space before newlines in content, to prevent breakage
    $content str_replace("\r\n"" \r\n"$content);

    $kwords explode(' '$keywords);
    $words explode(' '$content);

    $newcontent "";
    $resultwords = array();

    $count 1;

    foreach (
    $words as $w){
    //echo "W is $w";
        
    if (in_array($w$kwords)){
        
    // this is a keyword.. append link instead
        
    $newcontent .= "<a name=\"$w-".$count."\">$w</a> ";
        
    $resultwords["$w"] .= "<a href=\"#$w-".$count."\">$count</a>, ";
        
    $count++;
        }
        else {
        
    // normal word.. just append
        
    $newcontent .= "$w ";
        }

    Took a bit of hackiness, but it works.

  16. #16
    Join Date
    Aug 2006
    Location
    Costa Blanca, Spain
    Posts
    144
    Glad you sorted it!
    James Bone, citruszebra
    citruszebra.cz // 03333 404 100 // cz-at-citruszebra.cz

    Any views are my own and not of any company I may represent.

Posting Permissions

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