2012-04-12 56 views
1

不确定这是否已在之前得到解答 - 如何在两个关键字之间抓取字符串?如何在PHP中的两个关键字之间抓取字符串

例如'故事'和'”,

http://mywebsie.com/hello/blog/story/archieve/2012/5/?page=1  
http://mywebsie.com/blog/story/archieve/2012/4/?page=1 
http://mywebsie.com/blog/story/archieve/2012/?page=4 

我只是想,

story/archieve/2012/5/ 
story/archieve/2012/4/ 
story/archieve/2012/ 

编辑:

如果我使用parse_url

$string = parse_url('http://mywebsie.com/blog/story/archieve/2012/4/?page=1'); 
echo $string_uri['path']; 

我得到的,

/blog/story/archieve/2012/4/ 

,但我不希望包括 '博客/'

+0

要澄清,在结果中你想包括第一个关键字和排除第二个? – 2012-04-12 01:35:05

+0

只想包括第一个关键字不是第二...对不起,这不是很好的想法... – laukok 2012-04-12 01:38:06

+1

添加了一个答案,看看是否适合:) – 2012-04-12 01:44:20

回答

0

如果它是安全的假设,你正在寻找的子字符串输入字符串出现一次:

function getInBetween($string, $from, $to) { 
    $fromAt = strpos($string, $from); 
    $fromTo = strpos($string, $to); 

    // if the upper limit is found before the lower 
    if($fromTo < $fromAt) return false; 

    // if the lower limit is not found, include everything from 0th 
    // you may modify this to just return false 
    if($fromAt === false) $fromAt = 0; 

    // if the upper limit is not found, include everything up to the end of string 
    if($fromTo === false) $fromTo = strlen($string); 

    return substr($string, $fromAt, $fromTo - $fromAt); 
} 

echo getInBetween("http://mywebsie.com/hello/blog/story/archieve/2012/5/?page=1", "story", '?'); // story/archieve/2012/5/ 
+0

感谢您的回答。我得到了这个errro'解析错误:语法错误,意外的T_IF in ...'引用此行 - 'if($ fromTo === false)$ fromTo = strlen($ string);' – laukok 2012-04-12 01:50:56

+0

@lauthiamkok ups .. edited – 2012-04-12 01:51:26

+0

太棒了!它现在完美运作。感谢Nifty! :-) – laukok 2012-04-12 01:54:20

1

使用parse_url()

http://php.net/manual/en/function.parse-url.php

$parts = parse_url('http://mywebsie.com/story/archieve/2012/4/?page=1'); 
echo $parts['path']; 

您可以使用explode()或任何你提出的问题。

+0

感谢您的答案。请看看我的编辑。谢谢! – laukok 2012-04-12 01:08:06

+0

@lauthiamkok,对,你可以使用爆炸($ parts ['path'],'/',2)'或其他东西。不管你需要分割路径。 – Brad 2012-04-12 01:09:37

+0

谢谢布拉德。 'explode($ parts ['path'],'/',2)'定位在修复位置。如果我在'blog /'之前还有更多的东西比如'hello/blog /'怎么办? – laukok 2012-04-12 01:15:20

2

另一种非常简单的方法是,我们可以创建一个简单的功能,可以随时调用。

<?php 
    // Create the Function to get the string 
    function GetStringBetween ($string, $start, $finish) { 
    $string = " ".$string; 
    $position = strpos($string, $start); 
    if ($position == 0) return ""; 
    $position += strlen($start); 
    $length = strpos($string, $finish, $position) - $position; 
    return substr($string, $position, $length); 
    } 
?> 

,这里是你的问题

$string1="http://mywebsie.com/hello/blog/story/archieve/2012/5/?page=1";  
$string2="http://mywebsie.com/blog/story/archieve/2012/4/?page=1"; 
$string3="http://mywebsie.com/blog/story/archieve/2012/?page=4"; 

echo GetStringBetween ($string1, "/blog/", "?page"); 
//result : story/archieve/2012/5/ 

echo GetStringBetween ($string2, "/blog/", "?page"); 
//result : story/archieve/2012/4/ 

echo GetStringBetween ($string3, "/blog/", "?page"); 
//result : story/archieve/2012/ 

用法的例子更多细节,请阅读http://codetutorial.com/howto/how-to-get-of-everything-string-between-two-tag-or-two-strings

相关问题