简体   繁体   English

PHP,如果字符串包含数组中的任何字符

[英]php if string contains any character from an array

I have an xml document that gets loaded onto a page. 我有一个XML文档被加载到页面上。 Sometimes there are specific characters that cannot be parsed and shows this symbol in place of what should be there: – 有时是不能被解析并显示在地方应该有什么这个符号的特定字符: –

Sometimes the character varies from a hyphen, to an apostrophe, to even a double quote. 有时字符从连字符到撇号,甚至双引号都不同。

What I'd like to do is, create an array: 我想做的是创建一个数组:

$invalidCharacters = array(" – ", "'", "&");

and if the string contains any of those characters, replace them with their HTML/ASCII equivalent. 如果该字符串包含这些字符中的任何一个,则将其替换为等效的HTML / ASCII。 like this: " – " , "'" 像这样: " – ""'" , and & & .

I know that I can do a str_replace() on some items, but, is there a simple way to have it go trough a loop and look for the specific characters, replacing each as it goes? 我知道我可以在某些项目上执行str_replace() ,但是,有没有一种简单的方法可以让它经过一个循环并查找特定的字符,并随即替换每个字符?

Using htmlspecialchars should work for you. 使用htmlspecialchars应该适合您。

http://docs.php.net/manual/en/function.htmlspecialchars.php http://docs.php.net/manual/en/function.htmlspecialchars.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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