Jump to content

PHP Str_replace


MarkT

Recommended Posts

Hello,

I want to create a posting system for my website, where if the post contains a certain word, then it stops the SQL query that puts it into the database, or at least stops it.

 

Thanks

Link to comment
Share on other sites

You can use strpos() to see if the word exists in the string, though it doesn't take spaces into account.

if(strpos($value, 'word') === false) {    // If the word wasn't found then execute the SQL query}

Other solutions would be more complex.

Link to comment
Share on other sites

you can also use explode() to take out the sentence in seperate word splited by space. Make another array of your stop words and then you can sue in_array() to check if stop words exist in exploded words or not. if it is in array dont execute the sql.

http://php.net/expldoe

http://php.net/in_array

Link to comment
Share on other sites

You can use strpos() to see if the word exists in the string, though it doesn't take spaces into account.

if(strpos($value, 'word') === false) {    // If the word wasn't found then execute the SQL query}

Other solutions would be more complex.

Thanks

Link to comment
Share on other sites

you can also use explode() to take out the sentence in seperate word splited by space. Make another array of your stop words and then you can sue in_array() to check if stop words exist in exploded words or not. if it is in array dont execute the sql.

http://php.net/expldoe

http://php.net/in_array

Can you show me an example please, using the word; "hate" for example.

Link to comment
Share on other sites

Here's an example, but it would be more convenient for your career as a PHP programmer if you tried to piece things together on your own.

$sentence = 'I hate you';// Turn the sentence into an array of words$words = explode(' ', $sentence);// in_array() will tell you if a value was found in the array// Using the ! (NOT) operator we can tell if the value was NOT found in the arrayif( !in_array('hate', $words) ) {    // Execute the SQL query because the word was not found}
Link to comment
Share on other sites

 

Here's an example, but it would be more convenient for your career as a PHP programmer if you tried to piece things together on your own.

$sentence = 'I hate you';// Turn the sentence into an array of words$words = explode(' ', $sentence);// in_array() will tell you if a value was found in the array// Using the ! (NOT) operator we can tell if the value was NOT found in the arrayif( !in_array('hate', $words) ) {    // Execute the SQL query because the word was not found}

Is there a way to use multiple values for the in_array bit?

Link to comment
Share on other sites

If you check the link of in_array() manual you can see that you can even pass an array in first param.

Link to comment
Share on other sites

If you check the link of in_array() manual you can see that you can even pass an array in first param.

You can, but it would actually look for an array inside the other array, which wouldn't be useful for this purpose.

Link to comment
Share on other sites

You can, but it would actually look for an array inside the other array, which wouldn't be useful for this purpose.

 

Yes. it looks like you are right

 

 

 

 

Another way could be use array_intersect() with 'stop words' and 'word set'. check its return value if its empty match not found. if it matches anything it would return the array with element which have matched. Even it would be possible to track what it matched.

http://php.net/array_intersect

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...