how can I sort properly with preg_match?

℡╲_俬逩灬. 提交于 2019-12-25 11:50:54

问题


I'm trying to modify a line in PHP file that deals with data that comes from XML(about 600 stations). The php make this data usable by a streamer media player. The original line is this

if (($title <> "")&& (strpos($link,"<") === false)&& preg_match("/Cha/i",$lang)) 

this gives me about 50 stations, I want to add few stations that do not have "Cha" in $lang but "Soprts" and has "China" in their $title.

So I wrote the line like this

if (($title <> "")&& (strpos($link,"<") === false)&& preg_match("/Cha/i",$lang)&& (preg_match("/China/i",$title)||preg_match("/Sports/i",$lang)))

But now I'm getting less results than before, now I'm getting only the stations that have CHA in $lang and "China" in $title. All the relevant stations that have CHA in $lang but not "China" in $title are ruled out.

As I have very limited knowledge in PHP I don't know how to make the proper sorting, someone offered me to use stripos but I don't know how to use it and his example didn't worked OK


回答1:


You flipped the and and or in your new code. You want:

if (($title <> "")&& (strpos($link,"<") === false)&& (preg_match("/Cha/i",$lang)|| (preg_match("/China/i",$title)&&preg_match("/Sports/i",$lang))))



回答2:


You dont put bracets corretly. You can add multiple options in regex by using | (means OR) ,try this:

if (($title <> "") && (strpos($link,"<") === false) && preg_match("/CHA|China|Sports/i",$lang))


来源:https://stackoverflow.com/questions/9766140/how-can-i-sort-properly-with-preg-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!