从字符串中提取特定的字符串?

时间:2018-12-17 04:50:50

标签: php

我一直在尝试从字符串中获取特定的字符串,例如从下面的字符串中获取

Scale Lengths

4 string model - E A D G
5 string model - B E A D G
6 string model - B E A D G C

B   37″

我想要这部分

4 string model - E A D G
5 string model - B E A D G
6 string model - B E A D G C

下面是我的代码,我得到了正确的答案

<?php
$str = 'Scale Lengths

4 string model - E A D G
5 string model - B E A D G
6 string model - B E A D G C

B
37″';

echo getBetween($str,'Scale Lengths','37″');
function getBetween($content,$start,$end){
  $r = explode($start, $content);
  if (isset($r[1])){
      $r = explode($end, $r[1]);
      return $r[0];
  }
  return '';
}
 ?>

问题:我的问题是我上面的代码仅适用于此字符串的静态类型。例如,如果我们在此数字更改为37″之前将28″更改为BC之类的东西,它将开始给出错误的答案。

2 个答案:

答案 0 :(得分:1)

您可以这样做:

$str = 'Scale Lengths 4 string model - E A D G 5 string model - B E A D G 6 string model - B E A D G C B 37″';
preg_match_all('/\d+ string model \- [A-Z\s]+\s+/', $str, $m);
var_dump($m[0]);

live sample

编辑1

如果您没有换行符。

您可以这样做:

$str = 'Scale Lengths 4 string model - E A D G 5 string model - B E A D G 6 string model - B E A D G C B 37″';
preg_match_all('/(\d+) string model \- ([A-Z\s]+)\s+/', $str, $m);
$finded = [];
foreach($m as $index => $string) {
    $count = $m[1][$index];
    $oldMatched = $m[2][$index];
    // Base on the number capture before "string model", we valid if is what we expect.
    $t = preg_split("/[A-Z\s]{".($count * 2)."}/", $oldMatched);
    if(!empty($t[1])) {
        // if we find missmatch, we just fix it by removing unwanted chars.
        $newMatched = preg_replace("/".$t[1]."$/", '', $oldMatched);
        $finded[] = str_replace($oldMatched, $newMatched, $m[0][$index]);
    }
    else {
        $finded[] = $m[0][$index];
    }
}

live sample

编辑2

因为我以前的用法仍然捕获了不需要的字符。我们需要添加一些其他控件。

http://domain1.tld/random1/random11/random111/file1.jpg
http://domain2.tld/random12/file2.jpg
http://domain3.tld/file3.jpg
http://sub1.domain4.tld/blah/blahblah/file4.jpg

live sample

答案 1 :(得分:1)

尝试像这样更改代码:

<?php
$str = 'Scale Lengths

4 string model - E A D G
5 string model - B E A D G
6 string model - B E A D G C

B
37″';

$get_occurence=explode(" ",$str);
$split=preg_replace('/\s+/', '', end($get_occurence));
$split=preg_replace('/^[a-zA-Z]+/', '', $split);
$getBetween=getBetween($str,'Scale Lengths',$split);
$getLength=strlen($getBetween);
$getString=substr($getBetween,0,$getLength-3);

echo $getString;
function getBetween($content,$start,$end){
$r = explode($start, $content);
if (isset($r[1])){
  $r = explode($end, $r[1]);
  return $r[0];
}
return '';
}


?>

我已经编辑了一些代码,因此您可以根据需要动态替换B字符串。

<?php
$str = 'Scale Lengths

4 string model - E A D G
5 string model - B E A D G
6 string model - B E A D G C

B
37″';

$get_occurence=explode(" ",$str);
$split=preg_replace('/\s+/', '', end($get_occurence));
$split=preg_replace('/^[a-zA-Z]+/', '', $split);
$split_integer=preg_replace('/\s+/', '', end($get_occurence));
$split_integer=preg_replace('/[^a-zA-Z.]*/', '', $split_integer);
$getBetween=getBetween($str,'Scale Lengths',$split);
$getLength=strlen($getBetween);

$getString=substr($getBetween,0,$getLength-(strlen($split_integer)+1));

echo $getString;
function getBetween($content,$start,$end){
$r = explode($start, $content);
if (isset($r[1])){
  $r = explode($end, $r[1]);
  return $r[0];
}
return '';
}


?>

希望它对您有帮助。