1

これが以前に回答されているかどうかわからない-2つのキーワードの間の文字列を取得するにはどうすればよいですか?

たとえば、「ストーリー」と「? 」の間の文字列 '、

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/

しかし、私は' blog/ 'を含めたくありません

4

3 に答える 3

2

もう1つの非常に単純な方法は、いつでも呼び出すことができる単純な関数を作成できることです。

<?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

于 2014-12-16T23:28:40.553 に答える
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()そこから、または必要なものを何でも使用できます。

于 2012-04-12T01:02:00.793 に答える
0

探している部分文字列が入力文字列に1回だけ出現すると想定しても安全な場合は、次のようにします。

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/
于 2012-04-12T01:44:06.887 に答える