I try to remove an UTF-8 link in stirng
$old = array("سایت (english) :");
$new = array('');
$string = str_replace($old, $new, $string);
but no success ...can somebody please tell me my mistake?
Note I can remove pure non-english or pure english but not both in one text
try with this :
$string = mb_convert_encoding ($string, 'UTF-8');
$old = array (mb_convert_encoding ("سایت (english) :", 'UTF-8'));
$new = array ('');
$string = str_replace ($old, $new, $string);
Related
I am looking for a way to simply replace characters with their ASCII counterparts in MIME encoded emails. I've written preliminary code below, but it seems like the str_replace commands I'm using will keep on going forever to catch all possible combinations. Is there a more efficient way to do this?
<?php
$strings = "=?utf-8?Q?UK=20Defence=20=2D=20Yes=2C=20Both=20Labour=20and=20Tory=20Need=20To=20Be=20Very=20Much=20Clearer=20On=20Defence?=";
function decodeString($input){
$space = array("=?utf-8?Q?","=?UTF-8?Q?", "=20","?=");
$hyphen = array("=E2=80=93","=2D");
$dotdotdot = "=E2=80=A6";
$pound = "=C2=A3";
$comma = "=2C";
$decode = str_replace($space, ' ', $input);
$decode = str_replace($hyphen, '-', $decode);
$decode = str_replace($pound, '£', $decode);
$decode = str_replace($comma, ',', $decode);
$decode = str_replace($dotdotdot, '...', $decode);
return $decode;
}
echo decodeString($strings);
?>
I figured it out - I have to pass $strings to the mb_decode_mimeheader() function.
Hello friends have a little problem. I need to extract only the words of a text "anyone".
I tried to retrieve the words using strtok (), strstr (). some regular expressions, but only managed to extract some words.
The problem is complex due to the number of characters and symbols that can accompany the words.
The example text which must be extracted words. This is a sample text:
Main article: our 46,000 required, !but (1947-2011) mail#server.com March 8, 2014 Gutenberg's 34-DE 'a' 3,1415 Us: #unknown n go http://google.com or www.google.com and http://www.google.com (r) The 509th "composite" and; C-54 #dog v4.0 ¿as is done? ¿article... agriculture? x ¿cat? now! Hi!! (87 meters).
Sample text, for testing.
The result of extracting the text should be:
Main article our required but March Gutenberg's a go or and The composite and dog as is done article agriculture cat now Hi meters
Sample text for testing
The first function I wrote to facilitate the work
function PreText($text){
$text = str_replace("\n", ".", $text);
$text = str_replace("\r", ".", $text);
$text = str_replace("'", "", $text);
$text = str_replace("?", "", $text);
$text = str_replace("¿", "", $text);
$text = str_replace("(", "", $text);
$text = str_replace(")", "", $text);
$text = str_replace('"', "", $text);
$text = str_replace(';', "", $text);
$text = str_replace('!', "", $text);
$text = str_replace('<', "", $text);
$text = str_replace('>', "", $text);
$text = str_replace('#', "", $text);
$text = str_replace(",", "", $text);
$text = str_replace(".c", "", $text);
$text = str_replace(".C", "", $text);
return $text;
}
Split function:
function SplitWords($text){
$words = explode(" ", $text);
$ContWords = count($words);
for ($i = 0; $i < $ContWords; $i++){
if (ctype_alpha($words[$i])) {
$NewText .= $words[$i].", ";
}
}
return $NewText;
}
The program:
<?
include_once ('functions.php');
$text = "Main article: our 46,000 ...";
$text = PreText($text);
$text = SplitWords($text);
echo $text;
?>
Is that the code has a long way. We appreciate your help.
If I understand you correctly, you want to remove all non-letters from the string. I would use preg_replace
$text = "Main article: our 46,000...";
$text = preg_replace("/[^a-zA-Z' ]/","",$text);
This should remove everything that is not a letter, apostrophe or a space.
Try this almost your requirement
<?php
$text = <<<HEREDOC
Main article: our 46,000 required, !but (1947-2011) mail#server.com March 8, 2014 Gutenberg's 34-DE 'a' 3,1415 Us: #unknown n go http://google.com or www.google.com and
http://www.google.com (r) The 509th composite" and; C-54 #dog v4.0 ¿as is done? ¿article... agriculture? x ¿cat? now! Hi!! (87 meters). Sample text, for testing.
HEREDOC;
//replace all kind of URLs and emails from text
$url_email = "((https?|ftp)\:\/\/)?"; // SCHEME
$url_email .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?#)?"; // User and Pass
$url_email .= "([a-z0-9-.]*)\.([a-z]{2,4})"; // Host or IP
$url_email .= "(\:[0-9]{2,5})?"; // Port
$url_email .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
$url_email .= "(\?[a-z+&\$_.-][a-z0-9;:#&%=+\/\$_.-]*)?"; // GET Query
$url_email .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor
$text = preg_replace("/$url_email/","",$text);
//replace anything like Us: #unknown
$text = preg_replace("/Us:.?#\\w+/","",$text);
//replace all Non-Alpha characters
$text = preg_replace("/[^a-zA-Z' ]/","",$text);
echo $text;
?>
I am trying to remove extra whitespace of a string I get from internet using file_get_contents(). I tried str_replace() andpreg_replace(), and also search but none of them worked.
Here is my code:
<?php $html_content = file_get_contents("http://mindcity.sina.com.hk/MC-lunar/daily/2014/12/20141209_b5.html");
$html_content = mb_convert_encoding($html_content, 'UTF-8', 'BIG-5');
$html_content = strip_tags($html_content);
$start_pos = strrpos($html_content, "宜 :");
$end_pos = strrpos($html_content, "凶神宜忌 :") - strlen($html_content);
$good_to_do = substr($html_content, $start_pos, $end_pos);
echo $good_to_do .'<br>';
//remove whitespace of $good_to_do
$good_to_do = str_replace(' : ','*',$good_to_do);
$good_to_do = preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $good_to_do);
$good_to_do = str_replace(array("\r\n", "\r", "\n", "\t", "\0", "\s", "\x0B", "\x20", "\xA0"), '*', $good_to_do);
var_dump( $good_to_do ); ?>
Do
$good_to_do = preg_replace('/\s+/', '*', $good_to_do);
I put '*' because that is what you want to replace it with? You can put anything you like there.
Just found that the white space was when view source.
So the code just simply becomes str_replace(' ', '', $html_content);
Currently I have to call
$html = str_replace($search="\r\n", $replace='', $subject=$html);
$html = str_replace($search="\n", $replace='', $subject=$html);
to remove new line character in string $html. Is there a better/shorter way?
Try:
$html = str_replace(array("\r", "\n"), '', $html);
Yes, you can do that at once by using an array:
$search = array("\r\n", "\n");
$result = str_replace($search, $replace='', $subject=$html);
See str_replaceDocs.
http://domain.name/1-As Low As 10% Downpayment, Free Golf Membership!!!
The above url will report 400 bad request,
how to convert such title to user friendly good request?
You may want to use a "slug" instead. Rather than using the verbatim title as the URL, you strtolower() and replace all non-alphanumeric characters with hyphens, then remove duplicate hyphens. If you feel like extra credit, you can strip out stopwords, too.
So "1-As Low As 10% Downpayment, Free Golf Membership!!!" becomes:
as-low-as-10-downpayment-free-gold-membership
Something like this:
function sluggify($url)
{
# Prep string with some basic normalization
$url = strtolower($url);
$url = strip_tags($url);
$url = stripslashes($url);
$url = html_entity_decode($url);
# Remove quotes (can't, etc.)
$url = str_replace('\'', '', $url);
# Replace non-alpha numeric with hyphens
$match = '/[^a-z0-9]+/';
$replace = '-';
$url = preg_replace($match, $replace, $url);
$url = trim($url, '-');
return $url;
}
You could probably shorten it with longer regexps but it's pretty straightforward as-is. The bonus is that you can use the same function to validate the query parameter before you run a query on the database to match the title, so someone can't stick silly things into your database.
See the first answer here URL Friendly Username in PHP?:
function Slug($string)
{
return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}
$user = 'Alix Axel';
echo Slug($user); // alix-axel
$user = 'Álix Ãxel';
echo Slug($user); // alix-axel
$user = 'Álix----_Ãxel!?!?';
echo Slug($user); // alix-axel
You can use urlencode or rawurlencode... for example Wikipedia do that. See this link:
http://en.wikipedia.org/wiki/Ichigo_100%25
that's the php encoding for % = %25
I just create a gist with a useful slug function:
https://gist.github.com/ninjagab/11244087
You can use it to convert title to seo friendly url.
<?php
class SanitizeUrl {
public static function slug($string, $space="-") {
$string = utf8_encode($string);
if (function_exists('iconv')) {
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
}
$string = preg_replace("/[^a-zA-Z0-9 \-]/", "", $string);
$string = trim(preg_replace("/\\s+/", " ", $string));
$string = strtolower($string);
$string = str_replace(" ", $space, $string);
return $string;
}
}
$title = 'Thi is a test string with some "strange" chars ò à ù...';
echo SanitizeUrl::slug($title);
//this will output:
//thi-is-a-test-string-with-some-strange-chars-o-a-u
You could use the rawurlencode() function
To simplify just full the list of the variable $change_to and $to_change
<?php
// Just full the array list to make replacement complete
// In this space will change to _, à to just a
$to_change = [
' ', 'à', 'à', 'â','é', 'è', 'ê', 'ç', 'ù', 'ô', 'ö' // and so on
];
$change_to = [
'_', 'a', 'a', 'a', 'e', 'e', 'e','c', 'u', 'o', 'o' // and so on
];
$texts = 'This is my slug in êlàb élaboré par';
$page_id = str_replace($to_change, $change_to, $texts);