String replace PHP - php

Array {
[0] => http://abc.com/video/ghgh23;
[1] => http://smtech.com/file/mwerq2;
}
I want to replace the content between /sometext/ from the above array. Like I want to replace video, file with abc.

You don't need to loop over every element of the array, str_replace can take an array to replace with:
$myArray = str_replace(array('/video/', '/file/'), '/abc/', $myArray);
However, based on your question, you might want to replace the first path segment, and not a specific index. So to do that:
$myArray = preg_replace('((?<!/)/([^/]+)/)', '/abc/', $myArray);
That will replace the first path element of every URL in $myArray with /abc/...

One way is to use str_replace()
You can check it out here: http://php.net/str_replace

Either str_replace as other comments suggested or using a regular expression especially if you might have a longer url with more segments like http://example.com/xxx/somestuff/morestuff
In that case str_replace will not be enough you will need preg_replace

This is another option. Supply a array, foreach will pick it up and then the first parameter of str_replace can be a array if needed. Hope you find this helpful.
<?php
$array = array('http://abc.com/video/ghgh23','http://smtech.com/file/mwerq2');
$newarray = array();
foreach($array as $url) {
$newarray[] = str_replace(array('video','file'),'abc',$url);
}
print_r($newarray);
?>

//every element in $myArray
for($i=0; $i < count($myArray); $i++){
$myArray[$i] = str_replace('/video/','/abc/',$myArray[$i]);
}

$array = array('http://abc.com/video/ghgh23', 'http://smtech.com/file/mwerq2');
foreach ($array as &$string)
{
$string = str_replace('video', 'abc', $string);
$string = str_replace('file', 'abc', $string);
}

Related

How to get a range of arrays

I have a code which I have to explode my text using "*" as a delimiter.
I have a pattern that always the array [0] and [1] will be excluded and the rest of them need to be included inside a variable, but my problem is that I don't know how to catch dynamically the rest of the arrays that I have to put them all together inside of it.
Specially because my text may have more "*" and explode into more parts, but I have to get them all together. Excluding the [0] and [1]
$item= explode("*",$c7);
print_r($item);
//so now that I know which are my [0] and [1] arrays I need to get the rest of them inside of another variable
$variable = ?? //the rest of the $item arrays
$str = 'a*b*c*d*e';
$newStr = implode('*', array_slice(explode('*', $str), 2)); // OUTPUT: c*d*e
explode() is used to chunk the string by a delimiter
implode() is used to build a string again from chunks
array_slice() is used to select a range of the elements
I realise an answer was already accepted, but explode has a third argument for this, and with end you can grab that last, non-split part:
$str = 'a*b*c*d*e';
$res = end(explode("*", $str, 3));
$res gets this value as a result:
c*d*e
I think based off of your question, if I interpreted it correctly something like below will be useful.
USING A LOOP
$str = "adssa*asdASD*AS*DA*SD*ASD*AS*DAS*D";
$parts = explode("*", $str);
$newStr = "";
for ($i = 2; $i < count($parts); ++$i) {
$newStr .= $parts[$i];
}

Prepend each line of a variable with a string

How can I prepend a string, say 'a' stored in the variable $x to each line of a multi-line string variable using PHP?
Can also use:
echo preg_replace('/^/m', $prefix, $string);
The / are delimiters. The ^ matches the beginning of a string. the m makes it multiline.
demo
There are many ways to achieve this.
One would be:
$multi_line_var = $x.str_replace("\n", "\n".$x, $multi_line_var);
Another would be:
$multi_line_var = explode("\n", $multi_line_var);
foreach($multi_line_var AS &$single_line_var) {
$single_line_var = $x.$single_line_var;
}
$multi_line_var = implode("\n", $multi_line_var);
Or as a deceitfully simple onliner:
$multi_line_var = $x.implode("\n".$x, explode("\n", $multi_line_var));
The second one is dreadfully wasteful compared to the first. It allocates memory for an array of strings. It runs over each array item and modifies it. And the glues the pieces back together.
But it can be useful if one concatenation is not the only alteration you're doing to those lines of text.
Because of your each line requirement, I would first split the string to an array using explode, then loop through the array and add text to the beginning of each line, and then turn the array back to a string using implode. As long as the number of lines is not very big, this can be a suitable solution.
Code sample:
$arr = explode("\n", $x);
foreach ($arr as $key => $value) {
$arr[$key] = 'a' . $arr[$key];
}
$x = implode("\n", $arr);
Example at: http://codepad.org/0WpJ41LE

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);

How can I split a string?

i hv a string like
$q ="menu=true&submenu=true&pcode=123456&code=123456" ;
i want to get the value of pcode = 123456 and code = 123456
how i can get...?
Use parse_str function if it's not from url (then use $_GET array)
http://ru2.php.net/manual/en/function.parse-str.php
Use explode to get array from a string.
explode('&',$q);
It will explode string on every & character and return pieces in an array.
See parse_str
$q ="menu=true&submenu=true&pcode=123456&code=123456" ;
// parse str values into an array called $pairs
parse_str($q, $pairs);
// loop through $pairs and display all values
foreach ($pairs as $key => $val) {
echo "$key => $val\n";
}
Please use php explode functions to do it
ex:
<?php
$q ="menu=true&submenu=true&pcode=123456&code=123456" ;
$pieces = explode("&",$q);
print_r($pieces);
foreach($pieces as $key=>$value){
$newVarible[]=explode("=",$value);
}
print_r($newVarible);
?>

Categories