2012-03-04 11 views
11

PHPを使って改行を削除する関数を実行しましたが、コードを置き換えてtryしていますが、まだこれらの改行があります。これらの改行のためjqueryでjsonpから読んでください。PHPはラインブレイクまたはCR LFを削除しても成功しません

function clean($text) 
{ 
$text = trim(preg_replace('/\s+/', ' ', $text)); 
$text = preg_replace("/(\r\n|\n|\r|\t)/i", '', $text); 
return $text; 
} 

私はソースを見てみると、すべてのhref、IMGとBR にappeningいくつかの改行が、これはjson_encode出力 例があります:のafer

<a 
href=\"http:\/\/example.com\/out\/content\/\" title=\"link to content website\"> 

改行。それはIMG srcとBR

私は削除することができます唯一の方法にhapenigだ これらは

$text = preg_replace("/\s/i", '', $text); 

でそれを破るしかし、あなたはすべての文字列に残されたスペースがありませんことをunderstant、それは我々が望むものではありません。 JSONデ/シリアル化のタスクを処理するためのJSON拡張モジュールから

+0

感謝のすべてのために、今までは、これらのソリューションのどれも働きました。しかし、通常はこれらのソリューションの大部分はうまくいくはずですが、なぜ私のケースではそうでないのか理解できません – Gino

+0

修正するための唯一の方法は '$ allcontent = preg_replace( '/ Gino

+0

I hit the same and no solutions in this post worked for me. I found another similar post and this solution worked for me...hence posting it here: http://stackoverflow.com/a/3340510/405117 – Vikram

答えて

0

使用json_encode()json_decode()

$myobj = array('foo' => 'bar', 'foz' => 'baz') 

$json_myobj = json_encode($myobj); 
echo $json_myobj; 

$myobj = json_decode($json_myobj); 
print_r($myobj); 
25

このより良い私のために作品を置き換える:

= str_replace (array("\r\n", "\n", "\r"), ' ', $text) 
+0

As a note, in the case of e-mails and other MIME encodings, simply replacing '\r\n' (CRLF) with '\n' should work fine. –

0

をたぶんあなたは、テキスト文字を歩いてみてくださいそれぞれord()と呼んでください。したがって、これらの改行文字が実際に\r,\nですか?

最近、ASCIIテーブル(ordコード194など)内でさえも破られないスペースであることが判明した空白で同様の問題が発生しました。

$text = preg_replace("/[^ \na-zа-я0-9`~\[email protected]#\$%\^&\*\(\)_\+\-\=\[\]\{\}\\\|;\:'\",\.\/\<\>\?]+/ui", "", $text); 
+0

hi Tony, this did nothing for me – Gino

1

これはどう:

function clean($text) 
{ 
    $parts = explode(' ', $text); 
    foreach ($parts as $key => $value) 
     $parts[$key] = preg_replace('/\s/', ' ', $value); 
    return implode(' ', $parts); 
} 
あなたは私の解決策は、フィルタ休憩しようとするとしませんでしたが、そのようなテキストであることが予想されたもの以外のすべてをフィルタリングするために興味を持っている場合は

実際、このようなJSONファイルをクリーニングする代わりに、json_encodeを使用して作成すると、前の手順でこの問題を解決できます。

+0

Oops, the original code had a big mistake. I have edited right now... –

+0

not working, i don't understand why with all these replace code my json output give me 14 line instead of one. my json output work on many domains and many host, but for this domain (Wordpress website) it give me line break – Gino

0

私が使用する方法は、これは何echo str_replace(array('\r\n', '\r', '\n', '\t'), array('\\r\\n', '\\r', '\\n', '\\t'), $text);

されているテキストに改行を引き起こしているものを文字参照、およびそれらを適切に置き換えることができます。例えば、あなたが "\ n"あなたのテキストを壊している場合、このコードを使用すると、その場所に "\ n"が表示されます。例:

<a 
href=\"http:\/\/example.com\/out\/content\/\" title=\"link to content website\"> 

はなる:

当然
<a\n href=\"http:\/\/example.com\/out\/content\/\" title=\"link to content website\"> 

、そこに使用され得る他の破壊文字の大量であるが、\ R \ nは、\ R、\ nおよび\ tはあります最も一般的に使用されるもの。次

function clean($text) 
{ 
    return trim(preg_replace("/(\s*[\r\n]+\s*|\s+)/", ' ', $text)); 
} 

について

+0

it's crazy but nothing apear. by the way, the json_encode is called this way 'echo $_GET['jsoncallback'].'('.json_encode(array('posts' => $ posts) '; ' – Gino

1

どのように最初の部分\s*[\r\n]+\s*はそれがスペースをリードしていますし、それだけで1つのスペースにスペースを尾行だ、任意の改行に置き換えられます。

第2部分\s+はスペースを1つのスペースに縮小します。

trim()は、先頭/末尾のスペースを削除します。

0
function clean($text) 
{ 
    return trim(preg_replace('/\\\\r|\\\\n|\\\\t/i', ' ', $text)); 
} 

正常に動作します。

0

あなたはCRを削除してのLFを維持したい場合は、それは本当に(常識)非常に簡単です:

$text = str_replace("\r", "", $text); 
関連する問題