[英]PHP echo -> string as 0?
我如何避免以下情況:
$_SESSION['myVar']=preg_match("[^a-zA-Z]",'',$_SESSION['myVar']);
echo $_SESSION['myVar'];
顯示
0
而是顯示/輸出var內容? preg_match給出了混合類型,但這不應該是問題...
為什么字符串本身的值不能用echo尋址(通過共同映射其內容就可以了 )?
我以前有
$_SESSION['myVar']=ereg_replace("[^a-zA-Z]",'',$_SESSION['myVar']);
螞蟻的輸出ereg_replace被正確顯示的變量的內容。
PHP中的PCRE需要定界符[docs],而您可能需要preg_replace
[docs] :
preg_replace("/[^a-zA-Z]/",'',$_SESSION['myVar']);
假設您有preg_replace
,即使這樣,方括號( [...]
)也將被解釋為定界符,因此引擎將按字面意義嘗試在字符串開頭匹配a-zA-Z
,並且不會將構造方法解釋為字符類。
preg_match
返回一個整數,而不是混合整數: http : //php.net/manual/zh/function.preg-match.php
使用matchs參數獲取匹配項。
問題是preg_match返回一個布爾值,如果模式匹配則返回1,否則返回0。 preg_match只是匹配出現的內容,不會替換它們。 這是使用preg_match的方法:
$matched = array();
preg_match("/[^a-zA-Z]/", $_SESSION["myVar"], $matches);
print_r($matches); // All matches are in the array.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.