数组中的 PHP 文本字符串


PHP Text String in Array

我这里有一个简单的问题。我知道有一个非常简单的解决方案,但我无法弄清楚。所以我从用户那里获取文本输入并将它们存储在数组中,现在的问题是,如果文本中有逗号,,它会弄乱数组。

例如,如果用户输入:

Proin tincidunt velit turpis. Ut eu tempus tellus, vel dapibus sem ultrices sollicitudin justo lorem sit amit. Donec nec risus vulputate

输出为:

vel dapibus sem ultrices sollicitudin justo lorem sit amit. Donec nec risus vulputate

以下是我正在使用的代码:

$descText = explode( ",", $atts['desc'] );
// Store the descriptions in $textArray array.
foreach ($descText as $desc) {
    $textArray[]  = $desc;
}

更新:所以我在$descText中从用户那里获得了三个输入,所以如果其中任何一个偶然包含逗号,,那么它会忽略句子的其余部分。

你的意思是用户可以输入用逗号分隔的字符串。如果其中一个字符串包含逗号,它也会在该逗号上拆分,而您不希望这样做。

您需要使用不能出现在字符串中的其他分隔符,或者需要转义特殊字符,例如使用 ' .例如,如果您有以下字符串:

Hello, bye
Hello world

您将转义逗号,然后将它们组合在一起:

Hello', bye,Hello world

然后您可以使用 preg_split(..) .

$a = "Hello', bye,Hello world";
$b = preg_split( '/(?<!''''),/', $a );
var_dump( $b );

它将作为输出:

array(2) {
  [0]=>
  string(11) "Hello', bye"
  [1]=>
  string(11) "Hello world"
}

好的,我终于从问题中恢复过来了。我正在寻找的实际解决方案不多,但它可以完成工作。所以下面是这个问题的问题和解决方案。希望它对其他人有所帮助。

问题:

创建了一个WordPress短代码,其中我得到了两件事,一个音频链接和它的描述。这两个参数都将包含三个逗号分隔的参数。所以它会像这样:

[shortcode audio="one, two, three" desc="one, two, three"]

为了在一个变量中获得多个参数,我必须使用explode()以便可以将其分开。现在,如果其中一个 desc 偶然包含逗号,那么它被处理为不同的参数。所以以下内容给我带来了问题

[shortcode audio="one, two, three" desc="The text, The,text , The text"]

请注意 desc 变量中的第二个参数。它包含一个额外的逗号,因此作为单独的参数进行处理。

解决方案:

解决方案不是完美的,但它完成了工作。我所做的是将","分隔符替换为"*",所以现在我的短代码看起来像这样:

[shortcode audio="one * two * three" desc="The text * The,text * The text"]

守则:

function header_custom_box($atts) {
    $atts = shortcode_atts( array( 'audio' => '', 'desc' => ''), $atts, 'header-custom-box' );
    // Create Empty Arrays to store differnt mp3 links and descriptions.
    $posts = array();
    $audioArray = array();
    $textArray = array();
    $postCount = 3;
    // Load the Parameters
    $audioFiles = explode( "*", $atts['audio'] );
    $descText = explode( "*", $atts['desc'] );
    // Break if the parameters values are less than required.
    if ( count($audioFiles) < $postCount || count($descText) < $postCount) {
        echo "You need to provide atleast three links and descriptions. Please check the shortcode again!";
        exit;
    } 
    // Create audio with mp3 files in WordPress
    foreach ($audioFiles as $audioFile) {
        $attr = array(
        'src'      => trim($audioFile),
        'loop'     => '',
        'autoplay' => '',
        'preload' => 'none'
        );
        $audioArray[] = wp_audio_shortcode( $attr );
    }
    // Store the descriptions in $textArray array.
    foreach ($descText as $desc) {
        $textArray[]  = trim($desc);
    }
    // Format the post in HTML and store them in $posts array.
    $counter = 0;
    $buf = '';
    while ($counter < $postCount) { 
        $buf = '';
        $buf .= '<div class="header-tab-box">';
        $buf .= '<div class="audio-text"><h2>Your Daily Audio:</h2> <br/>';
        $buf .= $textArray[$counter];
        $buf .= '</div>';
        $buf .= '<div class="audio-player">';
        $buf .= $audioArray[$counter];
        $buf .= '</div>';
        $buf .= '</div>';
        $posts[$counter] = $buf;
        $counter++;
    }
    // Scheduling the returned posts.
    $currentDay = date("D");
    $postOne = $posts[0];
    $postTwo = $posts[1];
    $postThree = $posts[2];
    if ($currentDay == "Sat" || $currentDay == "Sun" || $currentDay == "Mon" || $currentDay == "Tue") {
        return $postOne;
    } elseif ($currentDay == "Wed" || $currentDay == "Thu") {
        return $postTwo;
    } elseif ($currentDay == "Fri" ) {
        return $postThree;
    }
}
add_shortcode( 'header-custom-box', 'header_custom_box' );

希望这有帮助!

谢谢大家..