Generate array with substring positions

Asked

Viewed 23 times

0

There’s some way I can pick up a string, like, "John went to the bar last night". Generate an array that stores the position of all " of that string.

I will use this to put in these positions for example, "no","de". Having so : "John was in the bar last night".

1 answer

1


Why not use the explode() in the string and turn every sentence into an array? Then reassemble the string with the new text?

<?php
// Example 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

Manual of the explosion http://php.net/manual/en/function.explode.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.