PHP found in string help

Chris S

Retired
Messages
2,055
Reaction score
1
Points
38
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
 

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
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:

primefalcon

New Member
Messages
15
Reaction score
0
Points
0
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:
<?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:

Chris S

Retired
Messages
2,055
Reaction score
1
Points
38
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:

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
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:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
Why not use explode to get an array from the list:
PHP:
<?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>';
?>
 

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
Why not use explode to get an array from the list:
PHP:
<?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 :)
 

Chris S

Retired
Messages
2,055
Reaction score
1
Points
38
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:

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
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 :)
 

Chris S

Retired
Messages
2,055
Reaction score
1
Points
38
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"
 
Top