开发者

Regex Problem - Getting what's inside the first span

开发者 https://www.devze.com 2023-01-12 19:15 出处:网络
I have some trouble with regex and php here: <span style=\"color: blue\">word1</span> word by word by word <span style=\"color: red\">word</span>

I have some trouble with regex and php here:

   <span style="color: blue">word1</span> word by word by word <span style="color: red">word</span>

I'm trying to get word1 out. Is the regex the best way though? Need to process around 70 sencences like this.

UPDATE

$one = '<spa开发者_如何学运维n style="color: blue">word1</span> word by word by word <span style="color: red">word</span>';
preg_match('/<span[^>]*>(.*?)<\/span>/',$one);
echo $one;

Don't works, it outputs the same. Am I doing something wrong?

Thanks


Using the following regex, capture the first result and discard the rest (if you just want to get the first result).

 <span[^>]*>(.*?)<\/span>

See it in action: http://rubular.com/r/ateGVj5PCu


WARNING: Regex is not suited for parsing HTML. If your code is more complicated than this, I strongly recommend using an (X)HTML parser


var str = '<span style="color: blue">word1</span> word by word by word <span style="color: red">word</span>
';

var myArray = str.match(/<span[^>]+>(\w+)<\/span>/);

Return myArray[1] = "word1";

0

精彩评论

暂无评论...
验证码 换一张
取 消