开发者

how do i use string replace to remove a carrage return from a string

Hi how do I use string replace to remove a carrage return from a string

here is the string:

{ $content = '<p>some random text <a href=
 "http://url.co.uk/link/">link text</a> some more random text.</p>'}

and here is the sring replace command

{$content=str_replace('carrage return', '', $content);}

The think I cant seem to find is what to replace 'carrage return' with. The reason for this is开发者_如何学JAVA that I am doing a reg expression on the string which fails if there is a carrage return in the serached for string as that then doesnt match the reg expression.

Also on an asside note if some one posts a response to my question how do I query them back on the posting.

second asside how the hell do i get html to show up properly on here ive tried '' "" () {} :(

be gentil im a newby and already got punched in the face on here :( second post and someone dropped a load of my score :(


This will replace the string "carriage return". I am pretty sure you want to replace the character defining a carriage return, which is \r. Also replace the newline char: \n as it is also defining a new line. ;)

$content = preg_replace("/\n|\r/", '', $content);


There are two chars which are used for line endings, \n (ASCII: 10, Unix/Linux) and \r (ASCII: 13, Mac OS). On windows the combination of those two is used \r\n. To get rid of all of them use something like this:

$str = str_replace(array("\n", "\r", "\r\n"), '', $input);


to replace use

carriage return

$content=str_replace("\r", '', $content);

new line

$content=str_replace("\n", '', $content);


$content = preg_replace("/\n|\r/", '', $content);


Try this

// Add all the carriage returns to the array that you wanted to replace
$carrage_returns = array("\r\n", "\n\r", "\n", "\r", "<br />");
$replace_with = ''; // empty string
$content = str_replace($carrage_returns, $replace_with, $content);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜