My code below is working
<?php
//load synonyms of words
$json_file = fopen("dict.json", "r") or die("Unable to open file!");
$js = fread($json_file, filesize("dict.json"));
$json = json_decode($js, true);
$text = "Hello my friend, today i am feeling good.";
$ar_text = explode(" ", $text);
foreach ($ar_text as $val)
{
$rand = rand(1, 3);
$randx = rand(1, 3);
if ($rand == $randx)
{
if (array_key_exists($val, $json))
{
$null = "{" . $val . "|";
$inc = $json[$val]['sinonim'];
$i = 1;
foreach ($inc as $siap)
{
$null .= $siap . "|";
if ($i == 4) break;
$i++;
}
$null .= "}";
$text = str_replace(" $val ", " $null ", $text);
//echo $null."<br>";
}
else
{
//echo "not found ".$val."<br>";
}
}
//echo $val;
}
$text = str_replace("|}", "}", $text);
echo $text;
//echo $json['mengaras']['tag'];
?>
i use explode to get word by word and then replace with word synonyms, how to get a phrase like "really good" and find it on dict.json.
Example: Hello, i am really good right now.
Output: Hello, i am {so fine|super fine|superb} now.
Use str_replace() - Replace all occurrences of the search string with the replacement string
$text = "Hello, i am really good right now.";
$find = "really good";
$replace = "so fine|super fine|superb";
$newtext= str_replace($find, $replace, $text);
OUTPUT:
Hello, i am so fine|super fine|superb right now.
You can do it this way too:
$text = 'Hello, i am really good right now.';
$match = 'really good';
$match_len = strlen($match);
$replace = array("so fine","super fine", "superb");
$pos = strpos($text, $match);
if($pos !== false){
echo "Word Found!";
$replace = implode("|", $replace);
$embed = '{' . $replace . '}';
echo $text_before . ' ' . $embed . ' ' . $text_after;
} else{
echo "Word Not Found!";
}
Output
Hello, i am {so fine|super fine|superb} right now.
Related
I have a working function that strips profanity words.
The word list is compose of 1700 bad words.
My problem is that it censored
'badwords '
but not
'badwords.' , 'badwords' and the like.
If I chose to remove space after
$badword[$key] = $word;
instead of
$badword[$key] = $word." ";
then I would have a bigger problem because if the bad word is CON then it will stripped a word CONSTANT
My question is, how can i strip a WORD followed by special characters except space?
badword. badword# badword,
.
function badWordFilter($data)
{
$wordlist = file_get_contents("badwordsnew.txt");
$words = explode(",", $wordlist);
$badword = array();
$replacementword = array();
foreach ($words as $key => $word)
{
$badword[$key] = $word." ";
$replacementword[$key] = addStars($word);
}
return str_ireplace($badword,$replacementword,$data);
}
function addStars($word)
{
$length = strlen($word);
return "*" . substr($word, 1, 1) . str_repeat("*", $length - 2)." " ;
}
Assuming that $data is a text that needs to be censored, badWordFilter() will return the text with bad words as *.
function badWordFilter($data)
{
$wordlist = file_get_contents("badwordsnew.txt");
$words = explode(",", $wordlist);
$specialCharacters = ["!","#","#","$","%","^","&","*","(",")","_","+",".",",",""];
$dataList = explode(" ", $data);
$output = "";
foreach ($dataList as $check)
{
$temp = $check;
$doesContain = contains($check, $words);
if($doesContain != false){
foreach($specialCharacters as $character){
if($check == $doesContain . $character || $check == $character . $doesContain ){
$temp = addStars($doesContain);
}
}
}
$output .= $temp . " ";
}
return $output;
}
function contains($str, array $arr)
{
foreach($arr as $a) {
if (stripos($str,$a) !== false) return $a;
}
return false;
}
function addStars($word)
{
$length = strlen($word);
return "*" . substr($word, 1, 1) . str_repeat("*", $length - 2)." " ;
}
Sandbox
I was able to answer my own question with the help of #maxchehab answer, but I can't declared his answer because it has fault at some area. I am posting this answer so others can use this code when they need a BAD WORD FILTER.
function badWordFinder($data)
{
$data = " " . $data . " "; //adding white space at the beginning and end of $data will help stripped bad words located at the begging and/or end.
$badwordlist = "bad,words,here,comma separated,no space before and after the word(s),multiple word is allowed"; //file_get_contents("badwordsnew.txt"); //
$badwords = explode(",", $badwordlist);
$capturedBadwords = array();
foreach ($badwords as $bad)
{
if(stripos($data, $bad))
{
array_push($capturedBadwords, $bad);
}
}
return badWordFilter($data, $capturedBadwords);
}
function badWordFilter($data, array $capturedBadwords)
{
$specialCharacters = ["!","#","#","$","%","^","&","*","(",")","_","+",".",","," "];
foreach ($specialCharacters as $endingAt)
{
foreach ($capturedBadwords as $bad)
{
$data = str_ireplace($bad.$endingAt, addStars($bad), $data);
}
}
return trim($data);
}
function addStars($bad)
{
$length = strlen($bad);
return "*" . substr($bad, 1, 1) . str_repeat("*", $length - 2)." ";
}
$str = 'i am bad words but i cant post it here because it is not allowed by the website some bad words# here with bad. ending in specia character but my code is badly strong so i can captured and striped those bad words.';
echo "$str<br><br>";
echo badWordFinder($str);
I want to search for a specific substring in given string and if found I want to simply insert some text in between in these strings in PHP.
I have used strpos
$text = "Something wordpress"
if(strpos($text,'wordpress',true) !== FALSE)
{
//Insert <br/> text
}
but dont know how to insert text in between in string.
Example output
Something <br/> Wordpress
$text = "Something wordpress";
if(strpos($text,'wordpress',true) !== FALSE)
{
$pos = strpos($text,'wordpress',true);
$text = substr($text, 0, $pos) . "new text " . substr($text, $pos);
}
echo $text;
Try using "str_replace" (http://php.net/manual/en/function.str-replace.php).
For example:
$text = "Something wordpress";
$searchText = "wordpress";
$replaceText = "different text";
$newPhrase = str_replace($healthy, $replaceText, $text);
$text = "Something wordpress";
if(strpos($text,'wordpress') !== false)
{
str_replace('wordpress', '<br>wordpress', $text);
}
$text = "Something wordpress";
if(strpos($text,'wordpress') !== false)
{
str_replace('wodrpress', ' your text ' . 'wordpress', $text);
}
This will do
echo str_replace('wordpress', '<br>wordpress', 'Something wordpress');
I wonder why no one mention substr_replace for insert..
$text = "Something wordpress";
$pos = strpos($text,'wordpress');
if($pos !== FALSE)
{
$text = substr_replace($text, "<br>", $pos, 0);
}
var_dump($text); // "Something <br>wordpress"
You could use a preg_match. This preg_match will search for 'wordpress' and replace it with 'wordpress'.
$search = 'wordpress';
$string = preg_replace('/' . preg_quote($search, '/') . '/', '<br />$0', $string);
However effective, my method makes no difference with the other answers whatsoever. It is simply a string replace action.
I have made a script to highlight a word in a string. The script is below.
function highlight_text($text, $words){
$split_words = explode( " " , $words );
foreach ($split_words as $word){
$color = '#FFFF00';
$text = preg_replace("|($word)|Ui", "<span style=\"background:".$color.";\">$1</span>", $text );
}
return $text;
}
$text = '*bc';
$words = '*';
echo highlight_text($text, $words);
When running the script, I got the following error:
Warning: preg_replace(): Compilation failed: nothing to repeat at offset 1
Can anyone help me?
This can help you:
<?php
function highlight_text($text, $words){
$split_words = explode( " " , $words );
foreach ($split_words as $key=>$word){
if (preg_match('/[\'^£$%&*()}{##~?><>,|=_+¬-]/', $word))// looking for special characters
{
$word = preg_quote($word, '/');// if found output \ before that
}
$color = '#FFFF00';
$text = preg_replace("|($word)|Ui", "<span style=\"background:".$color.";\">$1</span>", $text );
}
return $text;
}
$text = '*bc';
$words = '*';
echo highlight_text($text, $words);
change "*" to "\*" and profit.
You can check if special character in function highlight_text
like:
function highlight_text($text, $words){
$split_words = explode( " " , $words );
foreach ($split_words as $word){
$str = '';
$word = str_split($word);
foreach ($word as $c) {
if ($c == '*') {
$str .= '\*';
}
else {
$str .= $c;
}
}
$color = '#FFFF00';
$text = preg_replace("|($str)|Ui", "<span style=\"background:".$color.";\">$1</span>", $text );
}
return $text;
}
Change your code to
function highlight_text($text, $words){
$split_words = explode( " " , $words );
foreach ($split_words as $word){
$color = '#FFFF00';
$word = preg_quote($word, '/');
$text = preg_replace("|$word|Ui", "<span style=\"background:".$color.";\">$0</span>", $text );
}
return $text;
}
$text = '*bc';
$words = '*';
echo highlight_text($text, $words);
then will be ok.
I want to remove words from sentence if word contains #, I am using php.
Input: Hi I am #RaghavSoni
Output: Hi I am
Thank You.
You could do:
$str = preg_replace('/#\w+/', '', $str);
This is not a good way, but it works :
<?php
$input="Hi I am #RaghavSoni";
$inputWords = explode(' ', $input);
foreach($inputWords as $el)
{
if($el[0]=="#" )
{
$input = str_replace($el, "", $input);
}
}
echo $input;
?>
while(strpos($string, '#') !== false) {
$location1 = strpos($string, "#");
$location2 = strpos($string, " ", $location1);
if($location2 !== false) {
$length = $location2 - $location1;
$string1 = substr($string, 0, $location1);
$string2 = substr($string, $location2);
$string = $string1 . $string2;
}
}
echo $string;
echo str_replace("#RaghavSoni", "", "Hi I am #RaghavSoni.");
# Output: Hi I am.
I want to know how to count to the second space then return to the newline with php , for example :
the text: "How are you?"
becomes
"how are
you?"
how can I do that with php?
thank you
Here is a function that uses strpos to return the position of the Nth specified character.
http://us2.php.net/manual/en/function.strpos.php#96576
Find that position, then do a substr_replace to put a \n in that spot.
print preg_replace('/((:?\S+\s){2})/i', "\$1\r\n", "How are you?" );
function addNlToText($text) {
$words = explode(' ', $text);
$out = '';
foreach ($words as $key => $value) {
$out .= $value;
if ($key % 2 === 0) {
$out .= "\n";
} else {
$out .= ' ';
}
}
return trim($out);
}
That's dirty, but it does what you ask...
$text = 'Hello, how are you?';
addNlToText($text); // "Hello, how\nare you?"
$text = 'Hello';
addNlToText($text); // "Hello"
$text = 'Hello what is going on?';
addNlToText($text); // "Hello what\nis going\non?"
I wonder if you are not after something like the wordwrap function?
http://www.php.net/wordwrap
Try this:
<?php
$mystring = 'How are you?';
$findme = ' ';
$pos = strpos($mystring, $findme);
$pos = strpos($mystring, $findme, $pos+1);
$mystring = substr_replace($mystring, '<br>', $pos, 0);
echo $mystring;
?>
You can do it using javascript, Is it ok using javascript?