I have my class
public function convert( $title )
{
$nameout = strtolower( $title );
$nameout = str_replace(' ', '-', $nameout );
$nameout = str_replace('.', '', $nameout);
$nameout = str_replace('æ', 'ae', $nameout);
$nameout = str_replace('ø', 'oe', $nameout);
$nameout = str_replace('å', 'aa', $nameout);
$nameout = str_replace('(', '', $nameout);
$nameout = str_replace(')', '', $nameout);
$nameout = preg_replace("[^a-z0-9-]", "", $nameout);
return $nameout;
}
BUt I can't get it to work when I use special characters like ö and ü and other, can sombody help me here? I use PHP 5.3.
The first answer in this SO thread contains the code you need to do this.
And what about:
<?php
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>
From: http://php.net/manual/en/function.urlencode.php
I wrote this function a while ago for a project I was working on and couldn't get RegEx to work. Its not the best way, but it works.
function safeURL($input){
$input = strtolower($input);
for($i = 0; $i < strlen($input); $i++){
$working = ord(substr($input,$i,1));
if(($working>=97)&&($working<=122)){
//a-z
$out = $out . chr($working);
} elseif(($working>=48)&&($working<=57)){
//0-9
$out = $out . chr($working);
} elseif($working==46){
//.
$out = $out . chr($working);
} elseif($working==45){
//-
$out = $out . chr($working);
}
}
return $out;
}
Here's a function to help with what you're doing, it's written in
Czech: http://php.vrana.cz/vytvoreni-pratelskeho-url.php
(and translated to English)
Here's another take on it (from the Symfony documentation):
<?php
function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
// transliterate
if (function_exists('iconv'))
{
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
}
// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
if (empty($text))
{
return 'n-a';
}
return $text;
}
Related
I want the word between characters
'#' ,'_'
Change to
$word
Example :
#Ali_ is a good boy, I and #Jan_ love him
change to
Ali is a good boy, I and Jan love him
Thanks for your response
$string = 'ae #ali er #hassan sss';
$string = preg_replace("/#(.+?) /is", "<a href='Profile/$1'>$1</a>", $string);
echo($string);
Tried and tested:
function replaceString($content = '') {
preg_match_all('/#(.*?)_/', $content, $matches);
if(is_array($matches[0])) {
foreach($matches[0] as $key => $match) {
$new = $match;
$new = str_replace('#', '', $new);
$new = str_replace('_', '', $new);
$new = '' . $new . '';
$content = str_replace($match, $new, $content);
}
}
return $content;
}
The output of the following:
$content = '#name1_ and this other #name2_';
$content = replaceString($content);
echo $content;
Will be:
name1 and this other name2
I am trying to extract relative keywords from description input which use Wysiwyg, with multi language english/arabic… using the following function but its not doing the task I want. Have a look the function I am using:
function extractKeyWords($string) {
mb_internal_encoding('UTF-8');
$stopwords = array();
$string = preg_replace('/[\pP]/u', '', trim(preg_replace('/\s\s+/iu', '', mb_strtolower($string))));
$matchWords = array_filter(explode(' ',$string) , function ($item) use ($stopwords) { return !($item == '' || in_array($item, $stopwords)
|| mb_strlen($item) <= 2 || is_numeric($item));});
$wordCountArr = array_count_values($matchWords);
// <p><p>
arsort($wordCountArr);
return array_keys(array_slice($wordCountArr, 0, 10)); }
figured it out ! Thanks
function generateKeywords($str)
{
$min_word_length = 3;
$avoid = ['the','to','i','am','is','are','he','she','a','an','and','here','there','can','could','were','has','have','had','been','welcome','of','home',' ','“','words','into','this','there'];
$strip_arr = ["," ,"." ,";" ,":", "\"", "'", "“","”","(",")", "!","?"];
$str_clean = str_replace( $strip_arr, "", $str);
$str_arr = explode(' ', $str_clean);
$clean_arr = [];
foreach($str_arr as $word)
{
if(strlen($word) > $min_word_length)
{
$word = strtolower($word);
if(!in_array($word, $avoid)) {
$clean_arr[] = $word;
}
}
}
return implode(',', $clean_arr);
}
This one seems very nice and comprehensive https://www.beliefmedia.com.au/create-keywords
Just make sure to change this line
$string = preg_replace('/[^\p{L}0-9 ]/', ' ', $string);
to
$string = preg_replace('/[^\p{L}0-9 ]/u', ' ', $string);
To support other langages (e.g Arabic)
And also better to use mb_strlen
If the string is in html format you can add the
strip_tags($str);
Before
$min_word_length = 3;
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 use this code and all is ok, it hide text from $comments contains between [], but i want to hide text from other symbols for. ex. ** && ^^ $$ ## // <>. What i need to add here, to have INSTEAD OF
Date <20.02.2013> Time [11-00] Name #John#
Have this:
Date Time Name
?
function replaceTags($startPoint, $endPoint, $newText, $source) {
return preg_replace('#('.preg_quote($startPoint).')(.*)('.preg_quote($endPoint).')#si', '$1'.$newText.'$3', $source);
}
$source= $comments;
$startPoint='[';
$endPoint=']';
$newText='';
echo replaceTags($startPoint, $endPoint, $newText, $source);
You just need to change
$startPoint='[';
$endPoint=']';
to
$startPoint='<';
$endPoint='>';
To do multiple symbols you can do multiple calls to the function, like this:
$source= $comments;
$newText='';
$str = replaceTags('[', ']', $newText, $source);
$str = replaceTags('<', '>', $newText, $str);
$str = replaceTags('*', '*', $newText, $str);
$str = replaceTags('&', '&', $newText, $str);
$str = replaceTags('^', '^', $newText, $str);
$str = replaceTags('$', '$', $newText, $str);
$str = preg_replace("/\#[^#]+#)/","",$str);
$str = replaceTags('/', '/', $newText, $str);
// add more here
echo $str;
You will have to create patterns for each pair:
$pairs = array(
'*' => '*',
'&' => '&',
'^' => '^',
'$' => '$',
'#' => '#',
'/' => '/',
'[' => ']', // this
'<' => '>', // and this pairs differently from the rest
);
$patterns = array();
foreach ($pairs as $start => $end) {
$start = preg_quote($start, '/');
$end = preg_quote($end, '/');
$patterns[] = "/($start)[^$end]*($end)/";
}
echo preg_replace($patterns, '', $s), PHP_EOL;
// not sure what you want here
// echo preg_replace($patterns, '$1' . $newText . '$2', $s), PHP_EOL;
I have this array :
$GivenString = array("world", "earth", "extraordinary world");
how to get 'unmatch' string of variables like this :
$string = 'hello, world'; // output = 'hello, '
$string = 'down to earth'; // output = 'down to '
$string = 'earthquake'; // output = ''
$string = 'perfect world'; // output = 'perfect '
$string = 'I love this extraordinary world'; // output = 'I love this '
thanks!
I think simple str_replace will help you
$GivenString = array("world", "earth", "extraordinary");
echo str_replace($GivenString, "", $string);
array_diff http://php.net/manual/en/function.array-diff.php
$tokens = explode(' ', $string);
$difference = array_diff($tokens, $GivenString);
str_replace will not help, as there are $string = 'earthquake'; // output = '' in example. Here's piece of code that will do your job done.
$GivenString = array("world", "earth", "extraordinary world");
foreach ($GivenString as &$string) {
$string = sprintf('%s%s%s', '[^\s]*', preg_quote($string, '/'), '[^\s]*(\s|)');
}
// case sensitive
$regexp = '/(' . implode('|', $GivenString) . ')/';
// case insensitive
// $regexp = '/(' . implode('|', $GivenString) . ')/i';
$string = 'earthquake';
echo preg_replace($regexp, '', $string);