【问题标题】:Return everything before a certain character返回某个字符之前的所有内容
【发布时间】:2014-05-18 11:58:18
【问题描述】:

我正在尝试在破折号之前返回我的标题中的所有内容,这是我的代码

$mystring = "Nymphomaniac: Volume 1 – Nimfomana Vol. I (2013) – filme online"; 
$mystring = str_replace(' ', '+', $mystring);
$mystring = str_replace('-', '(/', $mystring);
$parts1 = explode("(", $mystring); 
//break the string up around the "(" character in $mystring 

$mystrings = $parts1[0]; 
echo $mystrings;

输出是这样的

Output:

Nymphomaniac:+Volume+1+–+Nimfomana+Vol.+I+

我想要的是这个

Output:

Nymphomaniac:+Volume+1

我认为问题在于有 2 个破折号我已经尝试了所有方法但没有成功,感谢您的帮助

【问题讨论】:

    标签: php string explode


    【解决方案1】:

    试试这个。

    <?php
    
        $mystring = "Nymphomaniac: Volume 1 – Nimfomana Vol. I (2013) – filme online"; 
        $array = explode("–", $mystring);
        $string = str_replace(" ", "+", trim($array[0]));
    
        echo $string; //Prints Nymphomaniac:+Volume+1
    
    ?>
    

    Here's the demo

    【讨论】:

    • 为什么使用这个标题“Nymphomaniac: Volume 1 – Nimfomana Vol. I (2013) – filme online”而不使用这个标题“Banlieue 13 - Ultimatum - 2009 - Film Online”
    • @user3649605 “-”(破折号)和“-”(连字符)是两个不同的字符。您的代码中的文本使用破折号,而您发布的文本使用连字符。将我的答案中的破折号更改为连字符,它会起作用。
    猜你喜欢
    • 2016-06-01
    • 2019-02-17
    • 2011-03-12
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多