-2

I have a simple problem here. I know there is a very simple solution to it, but I can't figure it out. So I am getting text input from the user and storing them in an array, now the issue is that if there are commas , in the text it messes up the array.

Like for example if the user inputs:

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

The Output is:

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

Below is the code I am using:

$descText = explode( ",", $atts['desc'] );

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

UPDATE: So I am getting three inputs from the user in $descText so if by chance any one of them includes a comma , in it then it neglects the rest of the sentence.

Fahad Sohail
  • 1,818
  • 4
  • 21
  • 33
  • How are users inputting the descriptions? It looks like you're exploding one long string of descriptions delimited by a comma. Why? – vanamerongen Jan 29 '15 at 12:51
  • 1
    You're telling it to split the text on commas... what are you expecting to happen? – CD001 Jan 29 '15 at 12:51
  • As your code your logic is correct..you will get split string in array $textArray...what exactly error or output you are getting please explain. $textArray[0] = Proin tincidunt velit turpis. Ut eu tempus tellus $textArray[1] = vel dapibus sem ultrices sollicitudin justo lorem sit amit. Donec nec risus vulputate – Prashant M Bhavsar Jan 29 '15 at 12:52
  • Please edit your question and add a [MCVE](/help/mcve). There is nothing that outputs anything in your code, so I am unsure what the problem is. – Sumurai8 Jan 29 '15 at 12:53
  • @vanamerongen Yes I need to explode it with a delimiter because I am getting multiple inputs from user. @CD001 I know mate, but I have no choice I am getting multiple inputs from the user and to store them in array I need to explode them using `,` – Fahad Sohail Jan 29 '15 at 12:54
  • 1
    @Sumurai8 He's exploding the string by a comma, so "the output" he's showing is $textArray[1]. The problem is why would you use a comma delimiter in texts that should contain commas?! – vanamerongen Jan 29 '15 at 12:55
  • 1
    So use another delimiter, one that won't be used as much, maybe ; or ~ or anything but a comma. I would actually advise against that though if what you're delimiting is entire sentences. There is no reason why your input should be one long string. – vanamerongen Jan 29 '15 at 12:56
  • 1
    It is important to know in which format you are getting user input, based on that we can suggest you best way to store it in array.. – Prashant M Bhavsar Jan 29 '15 at 12:56
  • See the issue is: I am getting three inputs from the user in $descText so if by chance any one of them includes a comma , in it then it neglects the rest of the sentence. – Fahad Sohail Jan 29 '15 at 13:02
  • Please consult a magician, this is not the place for a computer program. If you get the input `Proin, tincidunt, velit, turpis, Ut, eu, tempus, tellus`, there is not correct answer on how to `explode` that into three parts! – deceze Jan 29 '15 at 13:07

2 Answers2

3

What you are saying is that the user can input strings seperated by a comma. If one of the strings contains a comma, it is split on that comma too, and you don't want that.

You need to either use a different delimiter that cannot appear in the string, or you need to escape special characters, for example with a \. For example, if you have the following strings:

Hello, bye
Hello world

You would escape the comma's, then combine them:

Hello\, bye,Hello world

Then you can split them using preg_split(..).

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

Which will have as output:

array(2) {
  [0]=>
  string(11) "Hello\, bye"
  [1]=>
  string(11) "Hello world"
}
Sumurai8
  • 20,333
  • 11
  • 66
  • 100
  • Hello, well I thought this should work but somehow its still not working. `$text = $atts['desc']; $b = preg_split( '/(?<!\\\\),/', $text ); var_dump( $b );` still outputs me http://take.ms/3O5Wr – Fahad Sohail Jan 29 '15 at 13:22
  • @FahadSohail Read the entire answer. [This question](http://stackoverflow.com/questions/10646142/what-does-it-mean-to-escape-a-string) gives more information about escaping. – Sumurai8 Jan 29 '15 at 13:39
1

Okay, finally I have recovered from the problem. Not much of an actual solution which I was looking for, but it would get the job done. So below is the problem and solution for the question. Hope it helps someone else.

The Problem:

I created a WordPress shortcode in which I was getting two things, an audio link and description for it. Both of these two parameters would contain three comma separated parameters. So it would be something like:

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

To get multiple parameters in one variable I had to use explode() so that it could be separated. Now if one of the desc by any chance contains a comma , then it was processed as different parameter. So the following created problems for me

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

Notice the second parameter in desc variable. It contains an extra comma so that was processes as a separate parameter.

The Solution:

The solution was not the perfect one but it got the job done. What I did was I replaced the ',' delimiter as '*' so now my shortcode looks something like this:

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

The Code:

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' );

Hope this helps!

Thanks all ..

Fahad Sohail
  • 1,818
  • 4
  • 21
  • 33