I have a file called: ニューヨーク・ヤンキース-チケット-200x225.jpg
I am able to successfully do this with my PHP code:
if (file_exists(ABSPATH . 'ニューヨーク・ヤンキース-チケット-200x225.jpg')) {
echo 'yes';
}
However, when I parse my content using DOMDocument, that same string is returned as: ãã¥ã¼ã¨ã¼ã¯ã»ã¤ã³ãã¼ã¹-ãã±ãã-200x225.jpg
How do I prevent this happening with the following code? Our application is internationalised so we need to accomodate all utf-8 characters:
$dom = new DOMDocument();
$dom->encoding = 'utf-8';
$dom->loadHTML($content);
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
if( $image->hasAttribute('srcset') ) continue;
echo $initImgSrc = $image->getAttribute('src');
if (!preg_match('/[_-]\d+x\d+(?=\.[a-z]{3,4}$)/', $initImgSrc)) continue;
$newImgSrc = preg_replace('/[_-]\d+x\d+(?=\.[a-z]{3,4}$)/', '', $initImgSrc);
if (strpos($newImgSrc, '/') === 0) {
$newImgPath = str_replace( '/wp-content', ABSPATH . 'wp-content', $newImgSrc);
} else {
$newImgPath = str_replace( get_home_url(), ABSPATH, $newImgSrc);
}
if (!file_exists($newImgPath)) continue;
echo 'yes';
$dom->saveXML($image);
$oldSrc = 'src="' . $initImgSrc . '"';
$newDataSrcSet = $initImgSrc . ' 1x, ' . $newImgSrc . ' 2x';
$newSrcWithSrcSet = $oldSrc . ' srcset="' . $newDataSrcSet .'"';
$content = str_replace( $oldSrc, $newSrcWithSrcSet, $content );
}
return $content;
This code works normally, just not with the Japanese characters. Any help would be immensely appreciated