How to extract only part of string in PHP?

前端 未结 3 1319
难免孤独
难免孤独 2021-01-22 18:51

I have a following string and I want to extract image123.jpg.

..here_can_be_any_length \"and_here_any_length/image123.jpg\" and_here_also_any_length
3条回答
  •  清酒与你
    2021-01-22 19:14

    You can use:

    if(preg_match('#".*?\/(.*?)"#',$str,$matches)) {
       $filename = $matches[1];
    }
    

    Alternatively you can extract the entire path between the double quotes using preg_match and then extract the filename from the path using the function basename:

    if(preg_match('#"(.*?)"#',$str,$matches)) {
        $path = $matches[1]; // extract the entire path.
        $filename =  basename ($path); // extract file name from path.
    }
    

提交回复
热议问题