2012-12-24 53 views
-1

span标签之间的URL我有以下包含一个HTML代码:检查是否有

<span rel="url">example.com</span> 
<span rel="url">example.net.pl [SOMETHING]</span> 
<span rel="url">[SOMETHING]imjustanexample.com</span> [..] 

的问题是,如果有一种方式来获得span标签之间的“URL”的字符串。例如。它应该得到以下内容:example.com,example.net.pl(没有[SOMETHING]字符串)和imjustanexample.com。我想我将不得不使用正则表达式来达到这个目的。

+2

你需要给予更多的具体的例子我很害怕,因为你的字符串到目前为止,不包含任何网址。 –

+0

你是对的,它不是我的意思,那些'example.com'.. – Scott

+0

让我换一种方式,'[something]'是什么?而'example.com'不是一个网址!一个url是'http:// example.com /'。 –

回答

-1

检出简单的HTML Dom解析器(here)。

有了它,你可以简单地访问DOM树上的元素。

你的问题可能与解决:

$html->find("span[rel=url]"); 

然后你可以简单地使用所有的元素和一些正则表达式这符合自己需求的循环。

0

尝试在JavaScript正则表达式,

/((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/ 

从跨度标签验证文本

0

我会走这条路(在正则表达式或只是PHP代码,就像你喜欢):

  1. 找到下一个“”
  2. 从它的结束到下一个(但不包括)空间或低于号<(无论哪一个先来)。
  3. 重复,直到没有任何匹配。

完成。如果正则表达式对您来说太复杂,您还可以使用字符串函数http://php.net/strings

0

应该工作:

$str = '<span rel="url">http://google.ca</span>'; 
$match = preg_match('#<span(.*)?>((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|"|\'|:|\<|$|\.\s)</span>#i', $str, $matches); 
if($match) 
    var_dump($matches); 
else 
    echo 'Nope<br />'; 

从正则表达式:https://stackoverflow.com/a/206087/1533203