0

Hi

I'm bit stuck on how to put a period in array.

This is my example:

<pre>
<?php
$str = 'I like to eat Strawberry Cheesecake.';
$words = preg_split('@([\W]+)@', $str);
var_dump($words);
?>
</pre>

This is how it's var_dump():

array(7) {
  [0]=>
  string(1) "I"
  [1]=>
  string(4) "like"
  [2]=>
  string(2) "to"
  [3]=>
  string(3) "eat"
  [4]=>
  string(10) "Strawberry"
  [5]=>
  string(10) "Cheesecake"
  [6]=>
  string(0) ""
}

On my array[6] there should be a period. How do I get that period?

Any Suggestions and explanation will help. I appreciate it. Thank!

4
Contributors
15
Replies
17
Views
4 Years
Discussion Span
Last Post by LastMitch
Featured Replies
  • 1
    diafol 3,669   4 Years Ago

    $x = "I like to think."; $f = preg_split("/ /",$x); print_r($f); Were you looking for a different outcome? Read More

  • 1
    diafol 3,669   4 Years Ago

    'Try before reply' - good motto too. :) I'm asking whether you can have the period stuck to the last entry or whether you want it as a separate item. Read More

  • try this mitch $str = 'I like to eat Strawberry Cheesecake.'; $str2 = str_replace("."," .",$str); $words = explode(" ", $str2); Read More

0

@AndreRet

Thanks for the reply and example. I will test it out.

0

@AndreRet

I try the example it didn't work.

Here is my example now:

<?php
IF(SUBSTR($string, -1) != '.') {$string.= '.';}
$str = 'I like to eat Strawberry Cheesecake.';
$words = preg_split('@([\W]+)@', $str);
var_dump($words);
?>

When I run the code it said: Undefined variable: string

When I made some adjustment:

<?php
if(substr($str, -1) != '.') {$str.= '.';}
$str = 'I like to eat Strawberry Cheesecake.';
$words = preg_split('@([\W]+)@', $str);
var_dump($words);
?>

When I run the code it said: Undefined variable: str

Any Suggestions and explanation will help. I appreciate it. Thanks!

1
$x = "I like to think.";
$f = preg_split("/ /",$x);
print_r($f);

Were you looking for a different outcome?

Votes + Comments
Thanks for the example!
0

@diafol

Thanks for the reply & example.

Were you looking for a different outcome?

Not sure yet. You mean I can to this in a different way? Maybe.

I gonna test out the code. Thanks

1

'Try before reply' - good motto too. :)

I'm asking whether you can have the period stuck to the last entry or whether you want it as a separate item.

1

try this mitch

$str = 'I like to eat Strawberry Cheesecake.';
$str2 = str_replace("."," .",$str);
$words = explode(" ", $str2);
Votes + Comments
Thanks for the example!
0

@diafol

The period appear that was neat!

<?php
$str = 'I like to eat Strawberry Cheesecake.';
$words = preg_split('/ /', $str);
var_dump($words);
?>

When I echo out:

array(6) {
  [0]=>
  string(1) "I"
  [1]=>
  string(4) "like"
  [2]=>
  string(2) "to"
  [3]=>
  string(3) "eat"
  [4]=>
  string(10) "Strawberry"
  [5]=>
  string(11) "Cheesecake."
}

About the different outcome. There's so much variation in regex.

What do I to make array[6] to have a period in it:

array(7) {
[0]=>
string(1) "I"
[1]=>
string(4) "like"
[2]=>
string(2) "to"
[3]=>
string(3) "eat"
[4]=>
string(10) "Strawberry"
[5]=>
string(10) "Cheesecake"
[6]=>
string(0) ""
}

I appreciate any suggestion & explanation!

Edited by LastMitch: grammer

0

@diafol

'Try before reply' - good motto too. :)

I'm asking whether you can have the period stuck to the last entry or whether you want it as a separate item.

Yes I want the period to appear in the array.

array[6]=>string(1)"."

array(7) {
[0]=>
string(1) "I"
[1]=>
string(4) "like"
[2]=>
string(2) "to"
[3]=>
string(3) "eat"
[4]=>
string(10) "Strawberry"
[5]=>
string(10) "Cheesecake"
[6]=>
string(0) ""
}

Edited by LastMitch: grammer

0

I don't understand why you need this. Can you give an explanation as to what you're trying to do, then I can get my head around it.

0

@vaultdweller123

Thanks for the reply and example. I will test it out.

0

@diafol

I don't understand why you need this. Can you give an explanation as to what you're trying to do, then I can get my head around it.

No, it's playing around with arrays & regex for practice.

0

@diafol
@vaultdweller123

It work! Thanks for being patience and helping me out with this issue. Thanks!

Now I learn that there's 2 options of doing this:

Option 1:

<pre>
<?php
$str = 'I like to eat Strawberry Cheesecake.';
$words = preg_split('/ /', $str);
var_dump($words);
?>
</pre>

When I echo it out:

array(6) {
  [0]=>
  string(1) "I"
  [1]=>
  string(4) "like"
  [2]=>
  string(2) "to"
  [3]=>
  string(3) "eat"
  [4]=>
  string(10) "Strawberry"
  [5]=>
  string(11) "Cheesecake."
}

Option 2:

<pre>
<?php
$str = 'I like to eat Strawberry Cheesecake.';
$str2 = str_replace("."," .",$str);
$words = explode(" ", $str2);
var_dump($words);
?>
</pre>

When I echo it out:

array(7) {
  [0]=>
  string(1) "I"
  [1]=>
  string(4) "like"
  [2]=>
  string(2) "to"
  [3]=>
  string(3) "eat"
  [4]=>
  string(10) "Strawberry"
  [5]=>
  string(10) "Cheesecake"
  [6]=>
  string(1) "."
}
0

@mitch oh i thought you want the period to go in another array? incase you want word by word i can simplify my code and remove the str_replace

 <?php
    $str = 'I like to eat Strawberry Cheesecake.';
    $words = explode(" ", $str);
    var_dump($words);
    ?>

the reason i put the str_replace is because you said you want the period into the array @_@

Edited by vaultdweller123

1

@vaultdweller123

Thanks for another option and explanation. You gave me a good example. Thanks!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.