Hi guys
I need to get the number after each v and echo it out
$var="v9v10v11v12";
What would be the best way to do this?
Hi guys
I need to get the number after each v and echo it out
$var="v9v10v11v12";
What would be the best way to do this?
Try str_replace(…) or implode(…) and explode(…) to get individual numbers into an array().
Example of explode …
<?php
$str="v9v10v11v12";
$arr=explode("v",$str);
foreach ($arr as $key=>$val) {
echo $key ." is ". $val ."<br>";
}
?>
or
<?php
$str="v9v10v11v12";
$arr=explode("v",$str);
var_dump($arr);
?>
Just beware that the first element of your array will be empty if you use explode on a string that begins with v.
You can handle this by ignoring the first element, [FPHP]array_shift[/FPHP]'ing it off the front of the array, or by using [FPHP]preg_split[/FPHP] with the appropriate flag instead;
foreach ($arr as $key=>$val) {
if($key != 0) { echo $key ." is ". $val ."<br>"; }
}
array_shift($arr);
$arr = preg_split("~v~",$str,-1,PREG_SPLIT_NO_EMPTY);