Member Avatar for LastMitch

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!

Recommended Answers

All 15 Replies

Try the following -

IF(SUBSTR($string, -1) != '.') {
    $string.= '.';
    }
Member Avatar for LastMitch

@AndreRet

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

Member Avatar for LastMitch

@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!

Member Avatar for diafol
$x = "I like to think.";
$f = preg_split("/ /",$x);
print_r($f);

Were you looking for a different outcome?

commented: Thanks for the example! +5
Member Avatar for LastMitch

@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

Member Avatar for 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.

try this mitch

$str = 'I like to eat Strawberry Cheesecake.';
$str2 = str_replace("."," .",$str);
$words = explode(" ", $str2);
commented: Thanks for the example! +0
Member Avatar for LastMitch

@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!

Member Avatar for LastMitch

@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) ""
}
Member Avatar for 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.

Member Avatar for LastMitch

@vaultdweller123

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

Member Avatar for LastMitch

@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.

Member Avatar for LastMitch

@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) "."
}

@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 @_@

Member Avatar for LastMitch

@vaultdweller123

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.