doucheng1944
doucheng1944
2016-07-16 10:33
浏览 29
已采纳

如何在字符串php中搜索字符串?

I'm searching one PHP function or other solution for my problem.

So, I have a string, this:

$string = 'dfdfdjdkfjsdklfjdksfjekrjekjfdlkfjsdlfdflokuki48(malac)kjdkfdjfkjkejrkerjer';

I'm searching one function, that it has the next parameters:

functionName ($string, $fromCharacters, $toCharacters);

And I run this function:

functionName ($string, 'lokuki48(', ')');

And I will get this: 'malac', or max. this: 'lokuki48(malac)'.

Do you know about PHP function or solution for my problem?

I hope you understand my problem.

Thank you!

图片转代码服务由CSDN问答提供 功能建议

我正在为我的问题搜索一个PHP函数或其他解决方案。

所以,我有一个字符串,这个:

  $ string ='dfdfdjdkfjsdklfjdksfjekrjekjfdlkfjsdlfdflokuki48(malac)kjdkfdjfkjkejrkerjer'; 
   
 
 <  p>我正在搜索一个函数,它有下一个参数: 
 
 
  functionName($ string,$ fromCharacters,$ toCharacters); 
   
 
 

我运行这个函数:

  functionName($ string,'lokuki48(',')'); 
    
 
 

我会得到这个:'malac',或max。 这个:'lokuki48(malac)'。

你知道我的问题的PHP函数或解决方案吗?

我希望你理解我的问题。

谢谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dpqvwgr74759
    dpqvwgr74759 2016-07-16 10:38
    已采纳

    try this,

    $string = 'dfdfdjdkfjsdklfjdksfjekrjekjfdlkfjsdlfdflokuki48(malac)kjdkfdjfkjkejrkerjer';
    if (strpos($string, 'malac') !== false) 
    {
        echo 'true';
    }
    else
    {
        echo 'false';
    }
    

    https://3v4l.org/SiPvO

    i hope it will be helpful.

    点赞 评论
  • dousong2967
    dousong2967 2016-07-16 10:44

    The strstr() function searches for the first occurrence of a string inside another string.

    Example

    Find the first occurrence of "world" inside "Hello world!" and return the rest of the string:

    <?php
       echo strstr("Hello world!","world");
    ?>
    
    点赞 评论
  • doushai4890
    doushai4890 2016-07-16 11:01

    Thanks Guys!

    I have found the solution:

    `$string = 'dfdfdjdkfjsdklfjdksfjekrjekjfdlkfjsdlfdflokuki48(fasza)kjdkfdjfkjkejrkerjer';
    
    
    function get_string_between($string, $start, $end){
        $string = " ".$string;
         $ini = strpos($string,$start);
         if ($ini == 0) return "";
         $ini += strlen($start);     
         $len = strpos($string,$end,$ini) - $ini;
         return substr($string,$ini,$len);
    }
    
    echo get_string_between($string,"lokuki48(",")");`
    
    点赞 评论
  • dongqing904999
    dongqing904999 2016-07-16 12:08

    If you love lókuki, use preg_match:

    <?php
    
    function search($string, $start, $end)
    {
        preg_match("'".$start."(.*)".$end."'sim", $string, $out);
    
        if(isset($out[1])) {
            return $out[1];
        }
    
        return false;
    }
    
    
    
    
    $string = 'dfdfdjdkfjsdklfjdksfjekrjekjfdlkfjsdlfdflokuki48(malac)jdkfdjfkjkejrkerjer';
    
    $start="lokuki48\(";
    $end="\)";
    $res = search($string, $start, $end);
    
    echo $res."
    
    ";
    
    
    
    
    $string = 'dfdfdjdkfjsdklfjdksfjekrjekjfdlkfjsdlfdftestfunction(param1, param2)jdkfdjfkjkejrkerjer';
    
    $start="testfunction\(";
    $end="\)";
    $res = search($string, $start, $end);
    
    echo $res;
    
    点赞 评论

相关推荐