PHP remove newline in string for both Linux and Windows - php

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.

Related

remove an utf-8 text in string with str_replace

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);

Replacing characters in MIME encoded emails

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.

Cannot remove extra whitespace in a string retrieved from internet in php

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);

converting turkish characters

i am getting data from website and i am saving to xml file. i want to replace all turkish characters because if not, i am getting weird characters when i access the data from xml.
i used this code
$html = str_replace("ç", "Bccedil;", $html);
$html = str_replace("Ç", "BCcedil;", $html);
$html = str_replace("ö", "Bouml;" , $html);
$html = str_replace("Ö", "BOuml;" , $html);
$html = str_replace("ý", "B#305;" , $html);
$html = str_replace("Ý", "B#304;" , $html);
$html = str_replace("ü", "Buuml;" , $html);
$html = str_replace("Ü", "BUuml;" , $html);
$html = str_replace("ð", "B#287;" , $html);
$html = str_replace("Ð", "B#286;" , $html);
$html = str_replace("þ", "B#351;" , $html);
$html = str_replace("Þ", "B#350;" , $html);
$myFile = "test.xml";
$fh = fopen($myFile, 'w+') or die("can't open file");
fwrite($fh, $html);
fclose($fh);
it is weird that nothing changed after this code. if i want to replace for example 'a' to 'c' it works. but for turkish characters it doesnt work.
Data i get :
İstanbul Taksim’de başlayan ve yurt sathına yayılan olaylar
When i access :
Ýstanbul Taksim’de baþlayan ve yurt sathýna yayýlan olaylar
Can you try to do this when creating the xmldocument
$dom = new DOMDocument('1.0', 'utf-8');

Do multiple search and replaces in PHP string?

I am working with PHP and WordPress right now, I need to basically run the below code to Replace text in $current_path with the text in $new_path if $current_path EXIST in $content
I would prefer to be able to iterate over an array instead of running this over and over like this, or any better method would be nice?
$content = 'www.domain.com/news-tag/newstaghere'
$current_path = 'test-tag';
$new_path = 'test/tag';
$content = str_replace($current_path, $new_path, $content);
$current_path = 'news-tag';
$new_path = 'news/tag';
$content = str_replace($current_path, $new_path, $content);
$current_path = 'ppc-tag';
$new_path = 'ppc/tag';
$content = str_replace($current_path, $new_path, $content);
str_replace() accepts array arguments:
$current_paths = array('test-tag','news-tag','ppc-tag');
$new_paths = array('test/tag','news/tag','ppc/tag');
$new_content = str_replace($current_paths, $new_paths, $content);
Or you can use a single array with strtr():
$path_map = array('test-tag'=>'test/tag', 'news-tag'=>'news/tag', 'ppc-tag'=>'ppc/tag');
$new_content = strtr($content, $path_map);
However, you seem to be doing something very generic. Maybe all you need is a regex?
$new_content = preg_replace('/(test|news|ppc)-(tag)/u', '\1/\2', $content);
Or maybe even just
$new_content = preg_replace('/(\w+)-(tag)/u', '\1/\2', $content);
$content = 'www.domain.com/news-tag/newstaghere'
$current_paths = array('test-tag','news-tag','ppc-tag');
$new_paths = array('test/tag','news/tag','ppc/tag';
$content = str_replace($current_paths, $new_paths, $content);
Array arguments can be provided for the str_replace function, as noted on the following PHP.net page:
http://php.net/manual/en/function.str-replace.php
Please see "Example #2" on the page linked above for details.
You can do that:
$content = 'www.domain.com/news-tag/newstaghere';
$content = preg_replace('~www\.domain\.com/\w++\K-(?=tag/)~', '/', $content);

Categories