how to replace abusive words php - php

hello all i have a page where i need to replace all the occurrence of some particular words which may be abusive with a image (same image for all the words) and i have following function which does the work but it is case sensitive . like it replaces f*k but not f*k f*k f*k f**k etc.. and i think there is a better way to do this please help .
my function is
function filter($text) {
$icons = array(
'f**k ' => '<img src="smiles/filter.png" class="filter" title="Blured text "/>',
'something else' => '<img src="smiles/filter.png" class="filter" title="Blured text "/>',
' something else' => '<img src="smiles/filter.png" class="filter" title="f**k you ::F"/>'
);
return strtr($text, $icons);
}

You'd need a bigger filter based on RegEx for this
Like this:
function filterAbusiveWords( $string ) {
static $abusiveWords = array(
'/f[u]+c?k/i' => 'some icon',
'/f[\*]+c?k/i' => 'some icon',
'/f[a]+c?k/i' => 'some icon',
'/some normal word/i' => 'some icon'
);
return preg_replace( array_keys( $abusiveWords ), array_values( $abusiveWords ), $string );
}
This would (in theory) replace all occurences of fak, fuck, f**ck, f*k, faaak, fuk etc.

Related

Parsing html tags from php array in the response

Having the next array:
$link = 'Link ' . 'Learn more...';
$array = ['1' => 'normal string text', '2' => $link];
return $array;
The response would be:
1 "normal string text"
2 "Link, <a href="https://google.com">Learn more..."
The result expected would be that the a tag would get parsed:
Link, Learn more...
Tried with different solutions, but nothing seems to be working. Any ideas?
Thanks
Edited: The result should be an array in which one of the entries would be that link already parsed and ready to be used.
well you'r code is correct but you are returning the array outside a function
you need to add it to a function and call it
ex :
public function printArray(){
$link = 'Link ' . 'Learn more...';
$array = ['1' => 'normal string text', '2' => $link];
return $array;
}
//callback
echo printArray();
or if you want to pass some params
public function printArray($link , $text){
$link = $link;
$array = ['1' => $text, '2' => $link];
return $array;
}
//callback
$link = 'Link ' . 'Learn more...';
$text='normal string text';
echo printArray($link,$text);
or just print it like this:
$link = 'Link ' . 'Learn more...';
$array = ['1' => 'normal string text', '2' => $link];
print($array[0]);//output : normal string text
print($array[1]);//output : Link Learn more...
you can use :
$link = 'Link ' . 'Learn more...';
$elements = ['1' => 'normal string text', '2' => $link];
foreach($elements as $key => $element){
echo "{$key} {$element} <br>";
}
I don't know if this will work since I haven't tested it but you could try:
<?php
$array->data[] = array(
'Your text',
'<u>Learn more...</u>'
);
echo $array
?>

Replace variables from text with corresponding values

Let say I have the following string which I want to send as an email to a customer.
"Hello Mr/Mrs {{Name}}. You have subscribed for {{Service}} at {{Date}}."
And I have an array with the values that should be replaced
array(
'Name' => $customerName, //or string
'Service' => $serviceName, //or string
'Date' => '2015-06-06'
);
I can find all strings between {{..}} with this:
preg_match_all('/\{{(.*?)\}}/',$a,$match);
where $match is an array with values, But I need to replace every match with a corresponding value from an array with values
Note that the array with values contains a lot more values and the number of items in it or the keys sequence is not relevant to number of matches in string.
You can use preg_replace_callback and pass the array with the help of use to the callback function:
$s = "Hello Mr/Mrs {{Name}}. You have subscribed for {{Service}} at {{Date}} {{I_DONT_KNOW_IT}}.";
$arr = array(
'Name' => "customerName", //or string
'Service' => "serviceName", //or string
'Date' => '2015-06-06'
);
echo $res = preg_replace_callback('/{{(.*?)}}/', function($m) use ($arr) {
return isset($arr[$m[1]]) ? $arr[$m[1]] : $m[0]; // If the key is uknown, just use the match value
}, $s);
// => Hello Mr/Mrs customerName. You have subscribed for serviceName at 2015-06-06.
See IDEONE demo.
The $m[1] refers to what has been captured by the (.*?). I guess this pattern is sufficient for the current scenario (does not need unrolling as the strings it matches are relatively short).
You don't need to use a regex for that, you can do it with a simple replacement function if you change a little the array keys:
$corr = array(
'Name' => $customerName, //or string
'Service' => $serviceName, //or string
'Date' => '2015-06-06'
);
$new_keys = array_map(function($i) { return '{{' . $i . '}}';}, array_keys($corr));
$trans = array_combine($new_keys, $corr);
$result = strtr($yourstring, $trans);
Try
<?php
$str = "Hello Mr/Mrs {{Name}}. You have subscribed for {{Service}} at {{Date}}.";
$arr = array(
'Name' => 'some Cust', //or string
'Service' => 'Some Service', //or string
'Date' => '2015-06-06'
);
$replaceKeys = array_map(
function ($el) {
return "{{{$el}}}";
},
array_keys($arr)
);
$str = str_replace($replaceKeys, array_values($arr), $str);
echo $str;

Magento: improving search engine (inflections, irrelevant words removal, etc.)

I'm interested in knowing if I can detect inflections (e.g. dogs/dog), remove non-important words ("made in the usa" -> "in" and "the" are not important), etc. in the search string entered by the user for the Magento search engine without hard-coding such many scenarios in one big PHP code block. I can process this search string to a certain degree, but it will look unsanitary and ugly.
Any suggestions or pointers for making it an "intelliegent" search engine?
Use this class:
class Inflection
{
static $plural = array(
'/(quiz)$/i' => "$1zes",
'/^(ox)$/i' => "$1en",
'/([m|l])ouse$/i' => "$1ice",
'/(matr|vert|ind)ix|ex$/i' => "$1ices",
'/(x|ch|ss|sh)$/i' => "$1es",
'/([^aeiouy]|qu)y$/i' => "$1ies",
'/(hive)$/i' => "$1s",
'/(?:([^f])fe|([lr])f)$/i' => "$1$2ves",
'/(shea|lea|loa|thie)f$/i' => "$1ves",
'/sis$/i' => "ses",
'/([ti])um$/i' => "$1a",
'/(tomat|potat|ech|her|vet)o$/i'=> "$1oes",
'/(bu)s$/i' => "$1ses",
'/(alias)$/i' => "$1es",
'/(octop)us$/i' => "$1i",
'/(ax|test)is$/i' => "$1es",
'/(us)$/i' => "$1es",
'/s$/i' => "s",
'/$/' => "s"
);
static $singular = array(
'/(quiz)zes$/i' => "$1",
'/(matr)ices$/i' => "$1ix",
'/(vert|ind)ices$/i' => "$1ex",
'/^(ox)en$/i' => "$1",
'/(alias)es$/i' => "$1",
'/(octop|vir)i$/i' => "$1us",
'/(cris|ax|test)es$/i' => "$1is",
'/(shoe)s$/i' => "$1",
'/(o)es$/i' => "$1",
'/(bus)es$/i' => "$1",
'/([m|l])ice$/i' => "$1ouse",
'/(x|ch|ss|sh)es$/i' => "$1",
'/(m)ovies$/i' => "$1ovie",
'/(s)eries$/i' => "$1eries",
'/([^aeiouy]|qu)ies$/i' => "$1y",
'/([lr])ves$/i' => "$1f",
'/(tive)s$/i' => "$1",
'/(hive)s$/i' => "$1",
'/(li|wi|kni)ves$/i' => "$1fe",
'/(shea|loa|lea|thie)ves$/i'=> "$1f",
'/(^analy)ses$/i' => "$1sis",
'/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => "$1$2sis",
'/([ti])a$/i' => "$1um",
'/(n)ews$/i' => "$1ews",
'/(h|bl)ouses$/i' => "$1ouse",
'/(corpse)s$/i' => "$1",
'/(us)es$/i' => "$1",
'/s$/i' => ""
);
static $irregular = array(
'move' => 'moves',
'foot' => 'feet',
'goose' => 'geese',
'sex' => 'sexes',
'child' => 'children',
'man' => 'men',
'tooth' => 'teeth',
'person' => 'people',
'admin' => 'admin'
);
static $uncountable = array(
'sheep',
'fish',
'deer',
'series',
'species',
'money',
'rice',
'information',
'equipment'
);
public static function pluralize( $string )
{
global $irregularWords;
// save some time in the case that singular and plural are the same
if ( in_array( strtolower( $string ), self::$uncountable ) )
return $string;
// check for irregular singular forms
foreach ( $irregularWords as $pattern => $result )
{
$pattern = '/' . $pattern . '$/i';
if ( preg_match( $pattern, $string ) )
return preg_replace( $pattern, $result, $string);
}
// check for irregular singular forms
foreach ( self::$irregular as $pattern => $result )
{
$pattern = '/' . $pattern . '$/i';
if ( preg_match( $pattern, $string ) )
return preg_replace( $pattern, $result, $string);
}
// check for matches using regular expressions
foreach ( self::$plural as $pattern => $result )
{
if ( preg_match( $pattern, $string ) )
return preg_replace( $pattern, $result, $string );
}
return $string;
}
public static function singularize( $string )
{
global $irregularWords;
// save some time in the case that singular and plural are the same
if ( in_array( strtolower( $string ), self::$uncountable ) )
return $string;
// check for irregular words
foreach ( $irregularWords as $result => $pattern )
{
$pattern = '/' . $pattern . '$/i';
if ( preg_match( $pattern, $string ) )
return preg_replace( $pattern, $result, $string);
}
// check for irregular plural forms
foreach ( self::$irregular as $result => $pattern )
{
$pattern = '/' . $pattern . '$/i';
if ( preg_match( $pattern, $string ) )
return preg_replace( $pattern, $result, $string);
}
// check for matches using regular expressions
foreach ( self::$singular as $pattern => $result )
{
if ( preg_match( $pattern, $string ) )
return preg_replace( $pattern, $result, $string );
}
return $string;
}
public static function pluralize_if($count, $string)
{
if ($count == 1)
return "1 $string";
else
return $count . " " . self::pluralize($string);
}
}
And if you have a time use a standard way for inflection usage: http://en.wikipedia.org/wiki/Inflection
You can as array combine with XML so put all inflections data, look at how codeigniter has inflection very friendly: http://ellislab.com/codeigniter/user-guide/helpers/inflector_helper.html
Many frameworks supports built-in inflections but it will focus only in mainly English only. For other languages you should write own... or use unicode.org with some inflections standards for other languages if you need it.

How to add html to an array in php?

I am trying to add some html code before each tag printed as an array element.
My code:
$term_links = array();
foreach ($vars['node']->taxonomy as $term)
{
$term_links[] = l($term->name, 'taxonomy/term/' . $term->tid,
array(
'attributes' => array(
'title' => $term->description
)));
}
$vars['node_terms'] = implode(', ', $term_links);
At the moment the tags are printed seperated by a comma. I would like to add a small image before each tag element using img src="tag.png" How can I do this?
EDIT - My current Code, still not working.
if (module_exists('taxonomy')) {
$img = 'some html';
$text = $img . $term->name;
$path = 'taxonomy/term/' . $term->tid;
$term_links = array();
foreach ($vars['node']->taxonomy as $term) {
$term_links[] = l($text, $path, array(
'html' => TRUE,
'attributes' => array(
'title' => $term->description
)));
}
$vars['node_terms'] = implode(', ', $term_links);
}
}
Dupal's l() function has an option "html" you can set it to TRUE and use IMG + TITLE as a title.
Here is the example:
$img = '<img src="..." />';
$text = $img . $term->name;
$path = 'taxonomy/term/' . $term->tid;
$term_links[] = l($text, $path, array(
'html' => TRUE,
'attributes' => array(
'title' => $term->description
)
));

Add spaces inside array values

how can i make this code display $anchor with spaces. I would have Text Anchor1 and Text Anchor two insitead of TextAnchor1 and TextAnchor2. Thank you
$currentsite = get_bloginfo('wpurl');
$sites = array(
'TextAnchor1' => 'http://www.mysite1.com',
'TextAnchor2' => 'http://www.mysite2.com'
);
foreach($sites as $anchor => $site)
{
if ( $site !== $currentsite ){echo '<li>'.$anchor.'</li>';}
}
So, as you $anchor values probably aren't hard-coded, I assume what you really need is a function that takes a string as an argument and inserts spaces before any capital letters or numbers.
function splitWords($s){
return trim(preg_replace('/([A-Z0-9])/', ' \1', $s));
}
Later, when writing output, instead of $anchor, you can use splitWords($anchor).
$sites = array(
'Text Anchor 1' => 'http://www.mysite1.com',
'Text Anchor 2' => 'http://www.mysite2.com'
);
Ooh, ooh, my turn.
$sites = array(
'Text Anchor 1' => 'http://www.mysite1.com',
'Text Anchor 2' => 'http://www.mysite2.com'
);
Just change to:
$sites = array(
'Text Anchor' => 'http://www.mysite1.com',
'My Text Anchor' => 'http://www.mysite2.com'
);

Categories