简体   繁体   English

PHP的回声->字符串为0?

[英]PHP echo -> string as 0?

how would I avoid that the following : 我如何避免以下情况:

$_SESSION['myVar']=preg_match("[^a-zA-Z]",'',$_SESSION['myVar']);

echo $_SESSION['myVar'];

displays 显示

0

and instead it displays/outputs the var content ? 而是显示/输出var内容? preg_match gives out mixed type, but this shouldnt be the problem... preg_match给出了混合类型,但这不应该是问题...

Why, is the value of the string itself not addressable with echo (by comapring its contents, it is OK )? 为什么字符串本身的值不能用echo寻址(通过共同映射其内容就可以了 )?

Formerly I had 我以前有

$_SESSION['myVar']=ereg_replace("[^a-zA-Z]",'',$_SESSION['myVar']);

ant the output óf ereg_replace was correctly displayed the variable content. 蚂蚁的输出ereg_replace被正确显示的变量的内容。

PCRE in PHP need delimiters [docs] and you probably want preg_replace [docs] : PHP中的PCRE需要定界符[docs],而您可能需要preg_replace [docs]

preg_replace("/[^a-zA-Z]/",'',$_SESSION['myVar']);

Assuming you had preg_replace , even then, the brackets ( [...] ) would be interpreted as delimiters and so the engine would literally try to match a-zA-Z at the beginning of the string and would not interpret the constructor as character class. 假设您有preg_replace ,即使这样,方括号( [...] )也将被解释为定界符,因此引擎将按字面意义尝试在字符串开头匹配a-zA-Z ,并且不会将构造方法解释为字符类。

preg_match returns an int, not mixed: http://php.net/manual/en/function.preg-match.php preg_match返回一个整数,而不是混合整数: http : //php.net/manual/zh/function.preg-match.php

Use the matches parameter to get your matches. 使用matchs参数获取匹配项。

The problem is that preg_match returns a Boolean, 1 if the pattern was matched, 0 if it didn't. 问题是preg_match返回一个布尔值,如果模式匹配则返回1,否则返回0。 preg_match simply matches occurrences, it doesn't replace them. preg_match只是匹配出现的内容,不会替换它们。 Here's how you use 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM