English 中文(简体)
• 如何在PHP的两条关键词之间铺平
原标题:How to grab string between two keywords in PHP

不能确定这是否得到了答复——我如何在两个关键词之间 gr弄?

例如,story 之间的界限?,

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 ];

I get,

/blog/story/archieve/2012/4/

但我不想包括blog/

最佳回答

如果可以安全地假设,你所期望的底线在投入中一度出现:

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/
问题回答

另一种非常简单的方式是,我们可以创造一种简单的功能,可以在任何时候加以援引。

<?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-second-strings”rel=“nofollow”/>

使用<代码>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 ];

You can use explode() or whatever you need from there.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签