Reputation: 3
How could I remove all the new lines from all img tags. So for instance if I have:
$string = '<img
src="somelong
pathimage.jpg"
height="1" width="10">';
so it looks as:
$string = '<img src="somelongpathimage.jpg" height="1" width="10">';
Upvotes: 0
Views: 392
Reputation: 3464
If you indeed want to leave everything untouched but the guts of the img tags, the code bloats a little:
$string = "<html>\n<body>\nmy intro and <img\n src='somelong\npathimage.jpg'\n height='1' width='10'> and another <img\n src='somelong\npathimage.jpg'\n height='1' width='10'> before end\n</body>\n</html>";
print $string;
print trim_img_tags($string);
function trim_img_tags($string) {
$tokens = preg_split('/(<img.*?>)/s', $string, 0, PREG_SPLIT_DELIM_CAPTURE);
for ($i=1; $i<sizeof($tokens); $i=$i+2) {
$tokens[$i] = preg_replace("/(\n|\r)/", "", $tokens[$i]);
}
return implode('', $tokens);
}
Before:
<html>
<body>
my intro and <img
src='somelong
pathimage.jpg'
height='1' width='10'> and another <img
src='somelong
pathimage.jpg'
height='1' width='10'> before end
</body>
</html>
After:
<html>
<body>
my intro and <img src='somelongpathimage.jpg' height='1' width='10'> and another <img src='somelongpathimage.jpg' height='1' width='10'> before end
</body>
</html>
Upvotes: 0
Reputation: 1497
Because each OS have different ASCII chars for linebreak:
windows = \r\n
unix = \n
mac = \r
$string = str_replace(array("\r\n", "\r", "\n"), "", $string);
Thread link: http://www.php.net/manual/en/function.nl2br.php#73440
Upvotes: 2