I have these type of results from a loop function,
我有循环函数的这些类型的结果,
C:/wamp/www/xxx/core/page/
C:/wamp/www/xxx/local/page/
But how can I trim off anything before core or local, so I get these only,
但是如何在核心或本地之前修剪掉任何东西,所以我只能得到这些,
core/page/
local/page/
I use strstr
, but I think it search for a fixed keyword only, I have two, many more keywords to match,
我使用strstr,但我认为它只搜索一个固定的关键字,我有两个,更多的关键字匹配,
$string = 'C:/wamp/www/xxx/local/page/';
$output = strstr($string, '(local|core)');
var_dump($output);
I tried with preg_replace
,
我试过preg_replace,
var_dump(preg_replace('#/(core|local)/.*#si', '/', $string));
it gives me the front part - C:/wamp/www/xxx/
它给了我前面部分 - C:/ wamp / www / xxx /
6 个解决方案
#1
1
You can use preg_replace
like this:
您可以像这样使用preg_replace:
$output = preg_replace('~^.*?((?:core|local).*$)~i', "$1", $string);
or
$output = preg_replace('~^.*?(?=core|local)~i', '', $string);
If you want to match strictly up to the folder core
or local
, you can use this:
如果要严格匹配文件夹核心或本地,可以使用:
$output = preg_replace('~^.*?/(?=(?:core|local)/)~i', '', $string);
To your question:
对你的问题:
var_dump(preg_replace('#/(core|local)/.*#si', '/', $string));
This will match /(core|local)/.*
and replace it by /
, which is not really what you're looking for, because you actually have to match what is before this. My first regex here is an example of that: it will match everything before (?:core|local)
and then capture everything which comes afterwards into a capture group, which I'm referring to when using the backreference $1
.
这将匹配/(core|local)/.*并用/替换它,这实际上不是你想要的,因为你实际上必须匹配之前的内容。我的第一个正则表达式就是这样一个例子:它将匹配之前的所有内容(?:core | local),然后捕获随后进入捕获组的所有内容,我在使用反向引用$ 1时将其引用。
And well, because of the votewar going here... I added the forward slashes in the match, and you will be using less memory if you don't use a capture group at all (but using a lookahead), hence how I came to the last regex.
好吧,因为投票战在这里...我在比赛中添加了正斜杠,如果你根本不使用捕获组(但是使用前瞻),你将使用更少的内存,因此我是如何来的到最后一个正则表达式。
#2
2
Your code will be like:
你的代码就像:
$sData = 'C:/wamp/www/xxx/local/page/';
$sResult = preg_replace('/^(.*?)\/(core|local)\/(.*?)$/', '$2/$3', $sData);
#3
1
Use preg_match()
<?
$dir = "C:/wamp/www/xxx/core/page/";
preg_match("#^.*?/((?:local|core).*/)$#i",$dir,$match);
echo $match[1];
?>
If the first bit of the string is always the same you can useecho ltrim($dir, "C:/wamp/www/xxx/");
如果字符串的第一位始终相同,则可以使用echo ltrim($ dir,“C:/ wamp / www / xxx /”);
#4
0
$output = substr($string, strpos($string,'local/')+strpos($string,'core/'));
#5
0
You can do it in a different way using str_replace. Check below.
您可以使用str_replace以不同的方式执行此操作。检查下面。
$string = 'C:/wamp/www/xxx/local/page/';
$output = str_replace('C:/wamp/www/xxx/' , '' , $string);
var_dump($output);
It will simply trim off the left side string.
它只会修剪左侧的弦。
#6
-4
preg_replace('/.*(core|local)/', "$1", $string);
#1
1
You can use preg_replace
like this:
您可以像这样使用preg_replace:
$output = preg_replace('~^.*?((?:core|local).*$)~i', "$1", $string);
or
$output = preg_replace('~^.*?(?=core|local)~i', '', $string);
If you want to match strictly up to the folder core
or local
, you can use this:
如果要严格匹配文件夹核心或本地,可以使用:
$output = preg_replace('~^.*?/(?=(?:core|local)/)~i', '', $string);
To your question:
对你的问题:
var_dump(preg_replace('#/(core|local)/.*#si', '/', $string));
This will match /(core|local)/.*
and replace it by /
, which is not really what you're looking for, because you actually have to match what is before this. My first regex here is an example of that: it will match everything before (?:core|local)
and then capture everything which comes afterwards into a capture group, which I'm referring to when using the backreference $1
.
这将匹配/(core|local)/.*并用/替换它,这实际上不是你想要的,因为你实际上必须匹配之前的内容。我的第一个正则表达式就是这样一个例子:它将匹配之前的所有内容(?:core | local),然后捕获随后进入捕获组的所有内容,我在使用反向引用$ 1时将其引用。
And well, because of the votewar going here... I added the forward slashes in the match, and you will be using less memory if you don't use a capture group at all (but using a lookahead), hence how I came to the last regex.
好吧,因为投票战在这里...我在比赛中添加了正斜杠,如果你根本不使用捕获组(但是使用前瞻),你将使用更少的内存,因此我是如何来的到最后一个正则表达式。
#2
2
Your code will be like:
你的代码就像:
$sData = 'C:/wamp/www/xxx/local/page/';
$sResult = preg_replace('/^(.*?)\/(core|local)\/(.*?)$/', '$2/$3', $sData);
#3
1
Use preg_match()
<?
$dir = "C:/wamp/www/xxx/core/page/";
preg_match("#^.*?/((?:local|core).*/)$#i",$dir,$match);
echo $match[1];
?>
If the first bit of the string is always the same you can useecho ltrim($dir, "C:/wamp/www/xxx/");
如果字符串的第一位始终相同,则可以使用echo ltrim($ dir,“C:/ wamp / www / xxx /”);
#4
0
$output = substr($string, strpos($string,'local/')+strpos($string,'core/'));
#5
0
You can do it in a different way using str_replace. Check below.
您可以使用str_replace以不同的方式执行此操作。检查下面。
$string = 'C:/wamp/www/xxx/local/page/';
$output = str_replace('C:/wamp/www/xxx/' , '' , $string);
var_dump($output);
It will simply trim off the left side string.
它只会修剪左侧的弦。
#6
-4
preg_replace('/.*(core|local)/', "$1", $string);