[英]How to remove leading characters and the query character with a regular expression in PHP?
[英]remove all characters after a character using regular expression in php
我需要刪除在字符串中選擇的字符之后的所有字符。 這是我的繩子
$string = "Blow the fun in the sun.paste a random text";
$new_string = preg_replace("/paste.*/","",$string,-1,$count);
echo $new_string;
我的輸出是Blow the fun in the sun.
如果我的字符串是這樣的
$string = "Blow the fun in the sun.paste \n a random text";
$new_string = preg_replace("/paste.*/","",$string,-1,$count);
echo $new_string;
我的輸出是
Blow the fun in the sun.
a random text
但是,我需要輸出作為Blow the fun in the sun.
即使我的字符串中有\\n or \\t or some other special characters
。 在考慮那些特殊字符的同時,我該如何匹配?
您將需要s
標志(DOTALL)以使DOT匹配新行:
$new_string = preg_replace("/paste.*/"s, "", $string, -1, $count);
沒有s
標志,您的正則表達式與新行不匹配,因為您的輸入包含新行,並且您也想替換包含新行的字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.