字符串搜索和拆分

时间:2016-09-01 19:35:05

标签: php html

所以我有以下字符串

/home/design/Baie/Design-Interior-Baie-019.jpg

现在有办法搜索单词Baie并找回这样的字符串:

/home/design/Baie/

我试图建立一个面包屑

8 个答案:

答案 0 :(得分:0)

您可以使用dirname返回您要查找的内容

    echo dirname('/home/design/Baie/Design-Interior-Baie-019.jpg');

答案 1 :(得分:0)

var abcd = "/home/design/Baie/Design-Interior-Baie-019.jpg"
//Search
var n = abcd.search("Baie");
//split provide sub string
var xyz = abcd.split('Design');

consol.log(xyz[0])

或PHP

$abcd = "/home/design/Baie/Design-Interior-Baie-019.jpg";
$xyz = explode('Design', $abcd);
print_r($xyz)

答案 2 :(得分:0)

在PHP中,假设您的面包屑在第一次出现时结束了" Baie"在你的道路上:

$search = "Baie" ;
$path = "/home/design/Baie/Design-Interior-Baie-019.jpg" ;
$breadcrumb = substr($path, 0, stripos($path, $search . "/")+strlen($search)+1 );
print $breadcrumb ;

答案 3 :(得分:0)

$str = '/home/design/Baie/Design-Interior-Baie-019.jpg';
$search = 'Baie/';
$pos = strpos($str, $search);
$result = '';
if ($pos !== false) {
    $result = substr($str, 0, $pos + strlen($search));
}

print_r($result);

答案 4 :(得分:0)

使用dirname()功能

dirname('/home/design/Baie/Design-Interior-Baie-019.jpg');

或者您也可以尝试

<?php

$word = 'Baie'; 

$string = "/home/design/Baie/Design-Interior-Baie-019.jpg";

$bread =  explode( "$word", $string);

echo $bread[0]."/$word/";

?>

答案 5 :(得分:0)

在带有indexOf和slice的JavaScript中:

var str = "/home/design/Baie/Design-Interior-Baie-019.jpg";
var search = "Baie"
if (str.indexOf(search) != -1 )
   var result = str.slice(0,str.indexOf(search)+search.length+1);
console.log(result); // /home/design/Baie/

答案 6 :(得分:0)

正则表达式怎么样?

<?php
$array=[];
$path="/home/design/Baie/Design-Interior-Baie-019.jpg"
$pattern = "#^(.*/Baie)/.*$#";
preg_match($pattern,$path, $matchx);
echo $match[1];
?>

答案 7 :(得分:0)

如果您想要第一次使用:

$path = "/home/design/Baie/Design-Interior-Baie-019.jpg";
$find = "Baie";

$pos = strpos($path,$find);

$newstr = substr($path, 0, $pos + strlen($find));

echo $newstr;

另见:

http://php.net/manual/en/function.strpos.php

http://php.net/manual/en/function.substr.php