PHP正则表达式 - 找到单词并获取所有内容直到字符

时间:2017-04-14 00:28:06

标签: php regex character preg-match

我试图找到一个匹配字符串的正则表达式模式,并返回该字符串之后的所有内容,直到'/'。我目前有:

'/forums\/topic\/\s*([^\/]*)/u'

但这会返回包括'forums / topic /'

在内的所有内容

示例:string:

equest lorem ipsum dawhdk scripts. Problem: a href="__;_base_url_j;p_i;oj/index.php?/forums/topic/975-example-maps-to-local-rating/" then does something,

我只需要“975-example-maps-to-local-rating”

2 个答案:

答案 0 :(得分:1)

$pat = '=forums/topic/\s*([^/]*)=u';
$str = 'equest lorem ipsum dawhdk scripts. Problem: a href="__;_base_url_j;p_i;oj/index.php?/forums/topic/975-example-maps-to-local-rating/" then does something,';

preg_match($pat, $str, $match);

var_dump($match);

输出:

array(2) {
  [0]=>
  string(45) "forums/topic/975-example-maps-to-local-rating"
  [1]=>
  string(32) "975-example-maps-to-local-rating"
}

答案 1 :(得分:1)

你可以使用这样的正则表达式:

import IPython.html.widgets

<强> Working demo

Php代码

.*/(.*)/