I have an array in PHP $aResults full thousands of URLs that looks like this:
Array ( [0] => http://test.com/server1/Image?img=nortel.jpg )
Array ( [1] => http://test.com/server1/Image?img=network.jpg )
I need to replace the text inside each url and replace the server from server1 to server5 and the word image with photo so each url should look like this:
http://test.com/server5/photo?img=
How do i accomplish this?
I have tried a variation of str_replace functions but i cannot get this work:
$sImgURL = $aResults[1][0];
$filter_url ='server1/Image';
$replace='server5/photo';
$filtered_url = str_replace($filter_url, $replace, $aResults);
print_r($aResults);
What is the best way to accomplish this? Thanks
for ($i = 0; $i <= sizeof($aResults); $i++) {
$aResults[$i] = str_replace('server1/Image', 'server5/photo', $aResults[$i]);
}
You could use array_map and str_replace functions:
<?php
$data = array(
0 => 'http://test.com/server1/Image?img=nortel.jpg' ,
1 => 'http://test.com/server1/Image?img=network.jpg',
2 => 'http://test.com/server1/Image?img=rk.jpg',
3 => 'http://test.com/server1/Image?img=nek.jpg'
);
function foo($val){
return str_replace(
array('server1','Image'),
array('server5','photo'),
$val
);
}
$res = array_map("foo",$data);
var_dump($res);
?>
As Rizier123 suggest, you can do it in one line by:
var_dump(str_replace('server1/Image','server5/photo',$data));
Put your values to 'find' and your values to 'replace' into arrays..
$find = array('server1', 'Image');
$replace = array('server5', 'photo');
$newString = str_replace($find, $replace, $oldString);
Related
I have a php variable that contain value of textarea as below.
Name:Jay
Email:jayviru#demo.com
Contact:9876541230
Now I want this lines to in array as below.
Array
(
[Name] =>Jay
[Email] =>jayviru#demo.com
[Contact] =>9876541230
)
I tried below,but won't worked:-
$test=explode("<br />", $text);
print_r($test);
you can try this code using php built in PHP_EOL but there is little problem about array index so i am fixed it
<?php
$text = 'Name:Jay
Email:jayviru#demo.com
Contact:9876541230';
$array_data = explode(PHP_EOL, $text);
$final_data = array();
foreach ($array_data as $data){
$format_data = explode(':',$data);
$final_data[trim($format_data[0])] = trim($format_data[1]);
}
echo "<pre>";
print_r($final_data);
and output is :
Array
(
[Name] => Jay
[Email] => jayviru#demo.com
[Contact] => 9876541230
)
Easiest way to do :-
$textarea_array = array_map('trim',explode("\n", $textarea_value)); // to remove extra spaces from each value of array
print_r($textarea_array);
$final_array = array();
foreach($textarea_array as $textarea_arr){
$exploded_array = explode(':',$textarea_arr);
$final_array[trim($exploded_array[0])] = trim($exploded_array[1]);
}
print_r($final_array);
Output:- https://eval.in/846556
This also works for me.
$convert_to_array = explode('<br/>', $my_string);
for($i=0; $i < count($convert_to_array ); $i++)
{
$key_value = explode(':', $convert_to_array [$i]);
$end_array[$key_value [0]] = $key_value [1];
}
print_r($end_array); ?>
I think you need create 3 input:text, and parse them, because if you write down this value in text area can make a mistake, when write key. Otherwise split a string into an array, and after create new array where key will be odd value and the values will be even values old array
I am currently working on clean URL and stuck with this issue.
URL: http://localhost:8080/Pumps/Piston-pumps
$request_path = explode('?', $_SERVER['REQUEST_URI']);
$path_info = array_values($request_path);
echo str_replace('/',' ',$request_path[0]);
print_r($path_info);
above code gives below output :
Pumps Piston-pumps
Array ( [0] => /Pumps/Piston-pumps )
Is there a way I can convert the above array into:
Array ( [0] => Pumps [1] => piston-pumps )
I need this to add onto my URL
Thanks in advance
Here it is
<?php
$getUrl = "http://localhost:8080/Pumps/Piston-pumps"; // in your case it is $_SERVER['REQUEST_URI'];
$request_path = explode('?', str_ireplace(array("http://","https://"),"",$getUrl));
$expSlashes = explode("/", $request_path[0]);
$resultArr = array_slice($expSlashes, 1, count($expSlashes));
print_r($resultArr);
?>
use of explode and array_slice would result you with your required output.
explode is used to split a string with a needle sub-string
array_slice is used to create another array with limited and required number of array content
Try following code.
$new_array = ();
foreach($array as $k=>$v){
//This u split by upper case
$g = preg_split('/(?=[A-Z])/', $v);
$new_array[] = $g[0];
$new_array[] = strtolower($g[1]);
}
Use explode http://php.net/manual/en/function.explode.php
explode('/', '/Pumps/Piston-pumps');
To create array from 1 php variable Why out put not same array ?
in this code i create array using php variable
<?PHP
$xxx = "'Free', 'Include', 'Offer', 'Provide'";
$xxx_array = array($xxx);
echo '<pre>'; print_r($xxx_array); echo '</pre>';
?>
and echo is
Array
(
[0] => 'Free', 'Include', 'Offer', 'Provide'
)
how to echo like this
Array
(
[0] => Free
[1] => Include
[2] => Offer
[3] => Provide
)
<?php
$xxx = "'Free', 'Include', 'Offer', 'Provide'";
// Split by ","
$separatedValues = explode(',', $xxx);
// Remove the single quotation marks
for($i = 0; $i < count($separatedValues); ++$i) {
$separatedValues[$i] = str_replace("'", '', $separatedValues[$i]);
}
var_dump($separatedValues);
?>
It is all about the explode() (http://de.php.net/explode) function.
Read up on the explode() function. The below code accomplishes what you ask.
<?PHP
$xxx = "'Free', 'Include', 'Offer', 'Provide'";
$xxx_array = array(explode(",", $xxx);
echo '<pre>';
print_r($xxx_array);
echo '</pre>';
?>
If you want to mimic actually creating the array (and not having the values quoted within the array), use this:
$xxx_array = array_map( function( $el) { return trim( $el, "' "); }, explode(',', $xxx));
This trims the ' and spaces from the beginning and ends of the elements after converting the string to an array.
I have few functions that sanitize everything in get and post but this one must be bit more specific , I have an array that is producing links like
array(
1 => http://site/something/another/?get_something=1
2 => http://site/something/another2/?get_something=1
)
also have a function that is sanitizing get and post but that function would clear everything from this array values so I am left with
httpsitesomethinganotherget_something1
can someone please help to match EVERYTHING after get_something=
and clear it or replace it with get_something=1 , something like
$clear = preg_match('/^[A-Za-z0-9]+$/', $array[1]);
preg_replace("/get_something=".$clear."/i","",$array[1]);
You should use parse_url but here is a regex implementation, in case you're curious:
$urls = array
(
'http://site/something/another/?get_something=1',
'http://site/something/another2/?get_something=1',
'http://site/something/another/?get_something=1&foo=bar',
'http://site/something/another/?foo=bar&get_something=1',
);
$urls = preg_replace('~[?&]get_something=.*$~i', '', $urls);
print_r($urls);
Should output (demo [updated]):
Array
(
[0] => http://site/something/another/
[1] => http://site/something/another2/
[2] => http://site/something/another/
[3] => http://site/something/another/?foo=bar
)
If you're trying to parse the URL, your best bet is to use parse_url, lop off the query elements you don't want, and rebuild with http_build_url. Here's how I'd do it:
for($i = 0; $i < count($linkArray); $i++) {
// get original link and parse it
$link = parse_url($linkArray[$i]);
// replace the query portion
$link["query"] = "get_something=1";
// build the new URL
$linkArray[$i] = $link["scheme"] . "://" . $link["hostname"] . $link["path"] . "?" . $link["query"] . "#" . $link["fragment"]
// done
}
I have string:
ABCDEFGHIJK
And I have two arrays of positions in that string that I want to insert different things to.
Array
(
[0] => 0
[1] => 5
)
Array
(
[0] => 7
[1] => 9
)
Which if I decided to add the # character and the = character, it'd produce:
#ABCDE=FG#HI=JK
Is there any way I can do this without a complicated set of substr?
Also, # and = need to be variables that can be of any length, not just one character.
You can use string as array
$str = "ABCDEFGH";
$characters = preg_split('//', $str, -1);
And afterwards you array_splice to insert '#' or '=' to position given by array
Return the array back to string is done by:
$str = implode("",$str);
This works for any number of characters (I am using "#a" and "=b" as the character sequences):
function array_insert($array,$pos,$val)
{
$array2 = array_splice($array,$pos);
$array[] = $val;
$array = array_merge($array,$array2);
return $array;
}
$s = "ABCDEFGHIJK";
$arr = str_split($s);
$arr_add1 = array(0=>0, 1=>5);
$arr_add2 = array(0=>7, 1=>9);
$char1 = '#a';
$char2 = '=b';
$arr = array_insert($arr, $arr_add1[0], $char1);
$arr = array_insert($arr, $arr_add1[1] + strlen($char1), $char2);
$arr = array_insert($arr, $arr_add2[0]+ strlen($char1)+ strlen($char2), $char1);
$arr = array_insert($arr, $arr_add2[1]+ strlen($char1)+ strlen($char2) + strlen($char1), $char2);
$s = implode("", $arr);
print_r($s);
There is an easy function for that: substr_replace. But for this to work, you would have to structure you array differently (which would be more structured anyway), e.g.:
$replacement = array(
0 => '#',
5 => '=',
7 => '#',
9 => '='
);
Then sort the array by keys descending, using krsort:
krsort($replacement);
And then you just need to loop over the array:
$str = "ABCDEFGHIJK";
foreach($replacement as $position => $rep) {
$str = substr_replace($str, $rep, $position, 0);
}
echo $str; // prints #ABCDE=FG#HI=JK
This works by inserting the replacements starting from the end of string. And it would work with any replacement string without having to determine the length of that string.
Working DEMO