+ Reply to Thread
Results 1 to 10 of 10

Thread: PHP found in string help

  1. #1
    Chris S's Avatar
    Chris S is offline Retired Chris S is an unknown quantity at this point
    Join Date
    Mar 2005
    Posts
    1,036

    PHP found in string help

    I am working on something to help me find out if a song is explicative or not. I have the following code

    Code:
      if($i == 1 || $i == 2){
        $preLyrics = openLink($link);
        $lyrics = explode("Lyrics -->",$preLyrics);
        print_r($words);
        $length = count($words);
        for($x = 0;$x < $length; $x++){
          if(stristr($lyrics[1], $words[$x]) === TRUE){ 
            $found = true;
    	    break;
          }
        }
        if(!$found){
          echo '<pre>';
    	  print_r($lyrics[1]);
    	  echo '</pre>';
        }
      }
    the only issue is that its not returning the fact that the word is found. I am currently searching for the word 'both' (w/o the quotes) in the string:
    Good morning, son

    In twenty years from now

    Maybe we'll both sit down and have a few beers

    And I can tell you 'bout today

    And how I picked you up and everything changed

    It was pain

    Sunny days and rain

    I knew you'd feel the same things
    as you can see, the word 'both' appears in the string I am searching.

    Any solutions, I have one, but I don't really want to do it as it involves another loop

    I would love to change the world, but they won't give me the source code

  2. #2
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jun 2005
    Location
    Behind a keyboard.
    Posts
    8,998

    Re: PHP found in string help

    Working code here, modify as needed:
    Code:
    $explicit = array(
    "swear",
    "curse",
    "both",
    "omg",
    "wtf",
    "bbq"); //swear words, made x10 safe! :)
    $lyrics="my swearing mother."; //obviously change this so it gets the lyrics for you :)
    $isexplicit=false;
    foreach ($explicit as $checkme) {
    $pos=strpos($lyrics,$checkme);
    if ($pos!==FALSE) { $isexplicit=true;}
    }
    if ($isexplicit==true)
    {
        echo "Explicit.";
    }
    else {
        echo "not";
    }
    1 loop total and it'll do the same as yours.


    For the curious though, the problem is right here:

    Code:
       if(stristr($lyrics[1], $words[$x]) === TRUE){
    stristr doesn't return true. From PHP.net:

    Returns all of haystack from the first occurrence of needle to the end.
    *needle = what you're searching for.

    Boils down to the return value will never equal Boolean True - it'll either equal the string starting at needle to the end, or it'll return Boolean False if needle isn't found.

    strpos on the other hand will return an integer corresponding to the location of needle, or boolean false if it's not found (hence the !==FALSE in my code - if it's not returning boolean false, it's returning a location and the string was found).


    EDIT: In retrospect you could just replace the line I quoted as wrong with this:

    Code:
    if(strpos($lyrics[1], $words[$x]) !==FALSE)
    I had no honest idea what the rest of it was doing so I wrote up my own code for testing.
    Last edited by Livewire; 04-03-2009 at 10:34 PM.


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

  3. #3
    primefalcon is offline x10Hosting Member primefalcon is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    15

    Re: PHP found in string help

    Quote Originally Posted by Chris S View Post
    I am working on something to help me find out if a song is explicative or not. I have the following code

    Code:
      if($i == 1 || $i == 2){
        $preLyrics = openLink($link);
        $lyrics = explode("Lyrics -->",$preLyrics);
        print_r($words);
        $length = count($words);
        for($x = 0;$x < $length; $x++){
          if(stristr($lyrics[1], $words[$x]) === TRUE){ 
            $found = true;
            break;
          }
        }
        if(!$found){
          echo '<pre>';
          print_r($lyrics[1]);
          echo '</pre>';
        }
      }
    the only issue is that its not returning the fact that the word is found. I am currently searching for the word 'both' (w/o the quotes) in the string:


    as you can see, the word 'both' appears in the string I am searching.

    Any solutions, I have one, but I don't really want to do it as it involves another loop
    you know, if your just checking to see if it contains a word it'd be far easier just to do an eregi

    PHP Code:
    <?php
    $lyrics 
    "whatever your source for the lyrics are";

    eregi("both""$lyrics"))

    echo 
    "The word both has been found";
    //or whatever else you want to happen
    }
    ?>
    Last edited by primefalcon; 04-03-2009 at 10:46 PM. Reason: spelling error, lol

  4. #4
    Chris S's Avatar
    Chris S is offline Retired Chris S is an unknown quantity at this point
    Join Date
    Mar 2005
    Posts
    1,036

    Re: PHP found in string help

    this is weird...when I replace your list with my list, it stops working.

    Here is what I have.
    Code:
    <?php
    $explicit = file("list.txt"); //swear words, made x10 safe! :)
    echo '<pre>';
    print_r($explicit);
    echo '</pre>';
    $lyrics="my both mississippi."; //obviously change this so it gets the lyrics for you :)
    $isexplicit=false;
    foreach ($explicit as $checkme) {
    $pos=strpos($lyrics,$checkme);
    if ($pos!==FALSE) { $isexplicit=true;}
    }
    if ($isexplicit==true)
    {
        echo "Explicit.";
    }
    else {
        echo "not";
    }
    ?>
    the word I am searching for is both and here it is
    [1] => both

    In response to primefalcon:
    I tried what you gave me and after some modifying to get it to work, I came up with this

    Code:
    <?php
    $explicit = file("list.txt"); //swear words, made x10 safe! :)
    echo '<pre>';
    print_r($explicit);
    echo '</pre>';
    $lyrics="my 'both' mississippi."; //obviously change this so it gets the lyrics for you 
    foreach ($explicit as $checkme){
      echo "Searching for: $checkme <br />";
      if(eregi($checkme, $lyrics)){
    	echo "The word both has been found";
      }
    }
    ?>
    and yet nothing is working.

    If you really want to look at the list here it is. http://chris.justinandchris.com/list.txt
    Last edited by Chris S; 04-03-2009 at 11:08 PM.

    I would love to change the world, but they won't give me the source code

  5. #5
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jun 2005
    Location
    Behind a keyboard.
    Posts
    8,998

    Re: PHP found in string help

    Hmmm, I'll give it another go with that list shortly.

    Side note: I looked at the list, you've got a lot of entries that could be removed to save time during processing;

    For instance if I have moth listed as a curse word, I don't need to list moths or mother, because both contain the word "moth" - if moth isn't found, it win't find moths or mother either

    Just doing what I can bit by bit. I'll see if I can't figure out what's broken soon as I get back.
    Edit:
    Pardon the doublepost, I fixed the issue.

    The problem is actually list.txt - it's got newlines at the end of the words.

    Code:
    <?php
    $explicit = file("list.txt"); //swear words, made x10 safe! :)
    $badchars=array(
        "\r",
        "\n");
    echo '<pre>';
    print_r($explicit);
    echo '</pre>';
    $lyrics="my both mississippi."; //obviously change this so it gets the lyrics for you :)
    $isexplicit=false;
    foreach ($explicit as $checkme) {
    $pos=strpos($lyrics,str_replace($badchars,"",$checkme));
    if ($pos!==FALSE) { $isexplicit=true;}
    }
    if ($isexplicit==true)
    {
        echo "Explicit.";
    }
    else {
        echo "not";
    }
    ?>
    In short it's doing one last modification on $checkme - it's replacing all characters matching the list in $badchars with "blankspace," AKA absolutely zip, zero, nada



    I should point out that it's possible to use the same str_replace like this:

    Code:
    $lyrics=str_replace(str_replace($badchars,"",$explicit),"*censored*",$lyrics);
    Least this way it'll still display but it'll family-friendly it first

    Up to you either way, the second way's a heck of a lot shorter though. Would boil down to this:

    Code:
    <?php
    $explicit = file("list.txt"); //swear words, made x10 safe! :)
    $badchars=array(
        "\r",
        "\n");
    echo '<pre>';
    print_r($explicit);
    echo '</pre>';
    $lyrics="my both mississippi."; //obviously change this so it gets the lyrics for you :)
    $lyrics=str_replace(str_replace($badchars,"",$explicit),"*censored*",$lyrics);
    echo $lyrics;
    echo '</pre>';
    ?>
    Kinda does the same thing, but at least this way if the lyrics are explicit, it censors it all first.
    Last edited by Livewire; 04-04-2009 at 02:43 AM.


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

  6. #6
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: PHP found in string help

    Why not use explode to get an array from the list:
    PHP Code:
    <?php
    $explicit 
    file("list.txt"); //swear words, made x10 safe! :)
    $badchars=array(
        
    "\r",
        
    "\n");
    $explicit str_replace($badchars,"\n",$explicit)
    $explicit explode("\n"$explicit);
    echo 
    '<pre>';
    print_r($explicit);
    echo 
    '</pre>';
    $lyrics="my both mississippi."//obviously change this so it gets the lyrics for you :)
    $lyrics=str_replace($explicit,"*censored*",$lyrics);
    echo 
    $lyrics;
    echo 
    '</pre>';
    ?>
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  7. #7
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jun 2005
    Location
    Behind a keyboard.
    Posts
    8,998

    Re: PHP found in string help

    Quote Originally Posted by xav0989 View Post
    Why not use explode to get an array from the list:
    PHP Code:
    <?php
    $explicit 
    file("list.txt"); //swear words, made x10 safe! :)
    $badchars=array(
        
    "\r",
        
    "\n");
    $explicit str_replace($badchars,"\n",$explicit)
    $explicit explode("\n"$explicit);
    echo 
    '<pre>';
    print_r($explicit);
    echo 
    '</pre>';
    $lyrics="my both mississippi."//obviously change this so it gets the lyrics for you :)
    $lyrics=str_replace($explicit,"*censored*",$lyrics);
    echo 
    $lyrics;
    echo 
    '</pre>';
    ?>
    Didn't think of that actually; both work, guess it's user preference in this case


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

  8. #8
    Chris S's Avatar
    Chris S is offline Retired Chris S is an unknown quantity at this point
    Join Date
    Mar 2005
    Posts
    1,036

    Re: PHP found in string help

    im not looking to censor the lyrics. I am part of a christian originzation and we played blankest year over the loud system by accident. If you don't know what song im talking about...look for the unedited lyrics and your understand why im doing this.

    would explode end up looking like this

    array(
    [0] => array (
    [0] => word
    [1] = ""
    [1] => array (
    [0] => word
    [1] = ""
    )

    but I think i am going to look at the solutions above to see what to do.
    Last edited by Chris S; 04-04-2009 at 04:51 PM.

    I would love to change the world, but they won't give me the source code

  9. #9
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jun 2005
    Location
    Behind a keyboard.
    Posts
    8,998

    Re: PHP found in string help

    Quote Originally Posted by Chris S View Post
    im not looking to censor the lyrics. I am part of a christian originzation and we played blankest year over the loud system by accident. If you don't know what song im talking about...look for the unedited lyrics and your understand why im doing this.

    would explode end up looking like this

    array(
    [0] => array (
    [0] => word
    [1] = ""
    [1] => array (
    [0] => word
    [1] = ""
    )

    but I think i am going to look at the solutions above to see what to do.
    Explode should end up like this:

    array(
    [0]=>"word"
    [1]=>"word2"
    )
    etc.

    In all honesty both explode and my "str_replace" thing'll work, it's just user preference. I say try both and if there's a noticeable performance issue between the two, use whichever one's better. If there's not, flip a coin


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

  10. #10
    Chris S's Avatar
    Chris S is offline Retired Chris S is an unknown quantity at this point
    Join Date
    Mar 2005
    Posts
    1,036

    Re: PHP found in string help

    thanks for all your help. I am actually going to leave my list how it is because I don't want it to trigger a song as explicit because it found the "ho" in the word "home"

    I would love to change the world, but they won't give me the source code

+ Reply to Thread

Similar Threads

  1. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  2. PHP and new accounts
    By idani in forum Free Hosting
    Replies: 1
    Last Post: 09-21-2008, 01:49 PM
  3. PHP Word Scrambler - String Questions
    By masterjake in forum Programming Help
    Replies: 2
    Last Post: 09-17-2008, 03:51 PM
  4. Important PHP Information
    By Bryon in forum News and Announcements
    Replies: 0
    Last Post: 11-21-2007, 02:08 PM
  5. "PHP Startup: Invalid Library" - Interesting error
    By javaguy78 in forum Free Hosting
    Replies: 5
    Last Post: 03-27-2007, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers