简体   繁体   English

PHP Regex-查找单词并获取所有字符

[英]PHP Regex - find word and get everything until character

Im trying to find a regex pattern that matches a string, and return everything after that string until '/'. 我试图找到与字符串匹配的正则表达式模式,并返回该字符串之后的所有内容,直到'/'。 I currently have: 我目前有:

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

but this returns everything including 'forums/topic/' 但这会返回所有内容,包括“ forums / topic /”

Example: 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,

I only need "975-example-maps-to-local-rating" 我只需要“ 975-example-maps-to-local-rating”

$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);

Output: 输出:

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

You can use a regex like this: 您可以使用以下正则表达式:

.*/(.*)/

Working demo 工作演示

Php code 邮递区号

$re = '~.*/(.*)/~';
$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_all($re, $str, $matches);

// Print the entire match result
var_dump($matches);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM