converting turkish characters - php

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

Related

Shorter way to strip all html, add shortcode, preg_repace

This works perfectly for what I'm using it for (auto meta description and og:description content for WordPress), but I wonder if there is a way to write it shorter/cleaner:
$content = $post -> post_content;
$content = wp_trim_words($content, 40, '...'); // 40 words
$content = trim(str_replace(' ','',$content));
$content = do_shortcode($content);
$content = html_entity_decode($content);
$content = strip_tags($content);
$content = preg_replace('/\s\s+/', '', $content);
Updated
Okay, I think this will do the trick. I wanted to be able to use it again and again and then I thought, hey maybe it's like js. I'm a visual designer in my brain so it takes me sometime to get the hang of this stuff.
function do_meta_description_cab() {
global $post;
$content = $post -> post_content;
$content = wp_trim_words($content, 40, '...'); // 40 words
$content = trim(str_replace(' ','',$content));
$content = do_shortcode($content);
$content = html_entity_decode($content);
$content = strip_tags($content);
$content = preg_replace('/\s\s+/', '', $content);
return $content;
}
//Usage: $content = do_meta_description_cab();
To strip all html opening and closing tags, you can use :
$content = preg_replace('/<[^>]+>|<\/[^>]+>/', '', $content);

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

PHP remove newline in string for both Linux and Windows

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.

preg_replace '</p>' with '<br />'?

I have my code removing the <p> starting tags, but now I want to replace the ending </p> tags with line breaks. How can I do this?
This is what I have:
$content = 'This is the content';
$newcontent = preg_replace("/<p[^>]*?>", "", $content);
$newcontent = preg_replace("</p>", "<br />", $newcontent);
use str_replace instead of preg_replace, so:
$content = '<p>This is a new content for missing slash</p>';
$newcontent = preg_replace("/<p[^>]*?>/", "", $content);
$newcontent = str_replace("</p>", "<br />", $newcontent);
$content = preg_replace('#<p(.*?)>(.*?)</p>#is', '$2<br/>', $content);

Dump XML Posts from 'php://input' to file

I'm trying to write a parser for a xml postback listener, but can't seem to get it to dump the xml for a sample. The API support guy told me to use 'DOMDocument', maybe 'SimpleXML'? Anyways here's the code: (thanks!)
<?php
$xml_document = file_get_contents('php://input');
$doc = new DOMDocument();
$doc->loadXML($xml_document);
$doc->save("test2/".time().".sample.xml").".xml");
?>
How about use this to create an XML file?
/**
* Will output in a similar form to print_r, but the nodes are xml so can be collapsed in browsers
*
* #param mixed $mixed
*/
function print_r_xml($mixed)
{
// capture the output of print_r
$out = print_r($mixed, true);
// Replace the root item with a struct
// MATCH : '<start>element<newline> ('
$root_pattern = '/[ \t]*([a-z0-9 \t_]+)\n[ \t]*\(/i';
$root_replace_pattern = '<struct name="root" type="\\1">';
$out = preg_replace($root_pattern, $root_replace_pattern, $out, 1);
// Replace array and object items structs
// MATCH : '[element] => <newline> ('
$struct_pattern = '/[ \t]*\[([^\]]+)\][ \t]*\=\>[ \t]*([a-z0-9 \t_]+)\n[ \t]*\(/miU';
$struct_replace_pattern = '<struct name="\\1" type="\\2">';
$out = preg_replace($struct_pattern, $struct_replace_pattern, $out);
// replace ')' on its own on a new line (surrounded by whitespace is ok) with '</var>
$out = preg_replace('/^\s*\)\s*$/m', '</struct>', $out);
// Replace simple key=>values with vars
// MATCH : '[element] => value<newline>'
$var_pattern = '/[ \t]*\[([^\]]+)\][ \t]*\=\>[ \t]*([a-z0-9 \t_\S]+)/i';
$var_replace_pattern = '<var name="\\1">\\2</var>';
$out = preg_replace($var_pattern, $var_replace_pattern, $out);
$out = trim($out);
$out='<?xml version="1.0"?><data>'.$out.'</data>';
return $out;
}
Im my application I posted all of the $_POST variables to it:
$handle = fopen("data.xml", "w+");
$content = print_r_xml($_POST);
fwrite($handle,$content);
fclose();

Categories