我有一个测试,其中用户将有标记,所以我需要文本中的url href像我有文本
Hello World <a class="red" contenteditable="false" href="http://domainname.com/demo/forums/profile/test.name">Test </a> this is creating a problem
//I need test.name from the url
$text = $post_data->discussion;
$name = preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $text, $match);
print_r($name);
任何人都可以帮助我,因为我无法做到这一点我没能制作正则表达式
答案 0 :(得分:1)
您可以检查锚标记,这样您将始终获取网址(可能您的字符串包含单独的网址)。
$str = 'Hello World <a class="red" contenteditable="false" href="http://domainname.com/demo/forums/profile/test.name">Test </a> this is creating a problem';
preg_match_all("/<a.*?href\s*=\s*['\"](.*?)['\"]/", $str, $res, PREG_SET_ORDER);
foreach($res as $key => $val) {
echo $val[1];
}
// http://domainname.com/demo/forums/profile/test.name
答案 1 :(得分:0)
这应该做你需要的:
$regex = '/(?<=href="http)s?:\/\/[\w\.\/]+?\K(\w+\.\w+)(?=">)/';
$string = 'Hello World <a class="red" contenteditable="false" href="http://domainname.com/demo/forums/profile/test.name">Test </a>';
preg_match_all($regex, $string, $matches);
var_dump($matches);
// Outputs
array(2) {
[0]=>
array(1) {
[0]=>
string(9) "test.name"
}
[1]=>
array(1) {
[0]=>
string(9) "test.name"
}
}
以下是其工作示例 - https://eval.in/762641