php split value with "w" - php

I have a returning values that give me double digits, tripple digits etc. I want to put a "w" in between those numbers. Any kind of help I get on this is greatly appreciated.
$value = "444";
I want it to give me back this:
$value = "4w4w4w";

You could convert the string into an array and them implode it:
echo implode("w", str_split("444")) . "w";

Lets say $inp holds your number.
$arr = str_split($inp);
$result = implode('w',$arr);

str_split() and implode() will help.
$var = "444";
$array = str_split($var);
$final = implode("w", $array);
In this case you $final = "4w4w4";, so you might want to append an extra "w" to the end.

Here's a short way to do it with regex:
$str = '444';
echo preg_replace ( '/(.)/', '$1w', $str );

try this
$value="444";
$out='';
for($i=0;$i<strlen($value);$i++){
$out.=$value[$i]. "w";
}
echo $out;
output
4w4w4w

Related

First value comma separated string

There will be a string with a single or multiple with no commas at all.
$str = "a, b";
How do I get the first value before the comma if it contains comma(s)? This is what I did.
if(preg_match('/[,]+/', $str, $f)) {
$firstVal = $f[0];
}
NOTE: Is /^[^,]+/ better suited?
You can use strtok:
$firstVal = strtok($str, ",")
There are several ways to achieve this.
Using substr() and strpos()
echo substr($str,0,strrpos($str,','));
Using explode()
$result = explode(',',$str);
echo $result[0];
Using strstr()
echo strstr($str, ',', true);
Explode, validate and print.
$values = explode(',',$str);
if(key_exists(0,$values) && trim($values[0])!=""){
echo $values[0];
}

How to split a string after every ]

I have a string in an array like format.
["1","Data","time"] ["2","Data","time"] ["3","Data","time"] ["4","Data","time"]
I am trying to split the code after every ] - The data inside the [] could vary everytime so i cant use str_split();
I want to keep all the brackets in tack so they dont cut off so i cant use explode
Thank You
Easy regex:
$s = '["1","Data","time"] ["2","Data","time"] ["3","Data","time"] ["4","Data","time"]';
preg_match_all('/\[[^\]]+\]/', $s, $m);
print_r($m[0]);
But actually it's almost json, so:
$s = '["1","Data","time"] ["2","Data","time"] ["3","Data","time"] ["4","Data","time"]';
$s = '[' . str_replace('] [', '],[', $s) . ']';
print_r(json_decode($s));
Maybe you have a fragment or modified json, so it may be easier if you have actual json.
$str = '["1","Data","time"] ["2","Data","time"] ["3","Data","time"] ["4","Data","time"]';
$newStr = trim($str,'[');
$newStr1 = trim($newStr,']');
$arr = explode('] [',$newStr1);
print_r($arr);
Easiest solution (if you don't want regular expression) would be using trim 1 char from both sides and then explode by ']['.
Example:
$string = "[123][456][789]";
$string = substr($string,1,strlen($string)-1);
$array = explode('][',$string);

Split php string and keep delimiter with the first output

I'm trying to split a php string in to parts the first one include the delimiter but the second one doesn't
$string = "abc==123";
What I want exactly is to get
$string['0'] = "abc==";
$string['1'] = "123";
Thanks for help
Simple enough
<?php
$string = explode("==", $string);
$string[0] .= "==";
?>
I believe you want the function strstr
http://us1.php.net/strstr
You can use PHP's explode function,
$data = "abc==123";
$result = explode("==", $data);
echo $result[0]. "==";
echo $result[1];

separate with commas

hey there I have this,
$following_user_id .= $row['following_user_id'];
and I get
44443344330
then I use the implode() function and seperate with commans
44,44,33,44,33,0,
but I don't want the last comma on the last number?
Is this possible?
$following_user_ids = array();
//loop this:
$following_user_ids[] = $row['following_user_id'];
$user_ids_string = implode(',',$following_user_ids);
You can split the string into an array of characters, then implode the array.
$array = preg_split('//', $following_user_id, -1, PREG_SPLIT_NO_EMPTY);
echo implode( ',', $array );
Collect your data into an array of strings and use the implode function:
$uids = array();
while($row = mysql_fetch_assoc($result)){
array_push($uids, $row['following_user_id']);
}
$following_user_id = implode(',', $uids);
Check implode: http://php.net/manual/en/function.implode.php
Code example: I'm assuming your using some sort of loop?
$arrUsers = new array();
... your loop code here ...
array_push($arrUsers, $row['following_user_id']);
... end loop code ..
$following_user_id = impload(",", $arrUsers);
Implode should not be inserting a comma at the end of that string there. Are you sure there isn't an empty string at the end of your array sequence?
Either way, to fix the string you have, just get rid of the last character of the string:
$concatUserIds = "44,44,33,44,33,0,";
$concatUserIds = substr($concatUserIds, 0, strlen($concatUserIds) - 1);
Further, if you're not going to be using the non-comma delimited number set, why don't you just add a comma every time you add a user id. That way you don't even have to use the implode function.
This works for me:
<?php
$following_user_id.= $row['following_user_id'];
$following_user_id=preg_replace('/(?<=\d)(?=(\d)+(?!\d))/',',',$following_user_id);
echo $following_user_id."<br>";
?>
Try using arrays, example
<?php
$arr = array();
$arr[] = 'foo';
$arr[] = 'bar';
echo implode(',', $arr);

How do I place a comma between each character in a string with PHP? [duplicate]

hey there I have this,
$following_user_id .= $row['following_user_id'];
and I get
44443344330
then I use the implode() function and seperate with commans
44,44,33,44,33,0,
but I don't want the last comma on the last number?
Is this possible?
$following_user_ids = array();
//loop this:
$following_user_ids[] = $row['following_user_id'];
$user_ids_string = implode(',',$following_user_ids);
You can split the string into an array of characters, then implode the array.
$array = preg_split('//', $following_user_id, -1, PREG_SPLIT_NO_EMPTY);
echo implode( ',', $array );
Collect your data into an array of strings and use the implode function:
$uids = array();
while($row = mysql_fetch_assoc($result)){
array_push($uids, $row['following_user_id']);
}
$following_user_id = implode(',', $uids);
Check implode: http://php.net/manual/en/function.implode.php
Code example: I'm assuming your using some sort of loop?
$arrUsers = new array();
... your loop code here ...
array_push($arrUsers, $row['following_user_id']);
... end loop code ..
$following_user_id = impload(",", $arrUsers);
Implode should not be inserting a comma at the end of that string there. Are you sure there isn't an empty string at the end of your array sequence?
Either way, to fix the string you have, just get rid of the last character of the string:
$concatUserIds = "44,44,33,44,33,0,";
$concatUserIds = substr($concatUserIds, 0, strlen($concatUserIds) - 1);
Further, if you're not going to be using the non-comma delimited number set, why don't you just add a comma every time you add a user id. That way you don't even have to use the implode function.
This works for me:
<?php
$following_user_id.= $row['following_user_id'];
$following_user_id=preg_replace('/(?<=\d)(?=(\d)+(?!\d))/',',',$following_user_id);
echo $following_user_id."<br>";
?>
Try using arrays, example
<?php
$arr = array();
$arr[] = 'foo';
$arr[] = 'bar';
echo implode(',', $arr);

Categories