So I wrote some code that should simply take this
title=title&description=description&image=(some image here)&color=ff0000
And return this
echo $meta["title"] //title
echo $meta["description"] //description
echo $meta["image"] //(some image here)
echo $meta["color"] //ff0000
Instead, it only returns title for some reason...
<?php
$url = $_SERVER["REQUEST_URI"];
$url = substr($url, 2);
$url = base64_decode($url);
// title=title&description=description&image=(some image here)&color=ff0000
// confusing part
parse_str($url, $meta);
?>
What about smth like this?
$string = 'title=title&description=description&image=(some image here)&color=ff0000';
$array = explode("&", $string);
$finalArray = [];
foreach ($array as $arr) {
$singleArr = explode("=",$arr);
$finalArray[$singleArr[0]] = $singleArr[1];
}
print("<pre>".print_r($finalArray,true)."</pre>");
Which will print out:
Array
(
[title] => title
[description] => description
[image] => (some image here)
[color] => ff0000
)
Instead of reinventing the wheel :) (thx #u_mulder)
$urlString = parse_url($string, PHP_URL_QUERY);
parse_str($urlString, $urlArray);
print_r($urlArray["image"]); // Or what ever paramneter, like
foreach($urlArray as $key => $value) {
echo $urlArray[$key];
}
Related
I have a variable which can send me data like:-
thumb_8_2393_Shades 1.jpg, hanger-cloth.jpg & Red-Lehenga-1.jpg;
Now, when the value will have 'thumb_' at the left side, I want to discard the 'thumb_' string from the full value.
So I wrote this code:-
$pImgBig = trim($pI['image'],'thumb');
What the issue I am facing is, it is also removing the 'h' from the 'hanger-cloth.jpg'.
How can I overcome this issue?
You can use preg_replace() like below:-
$pImgBig = preg_replace('/^thumb_/','',$pI['image']);
<?php
$data = 'hanger-cloth.jpg';
$data = preg_replace('/^thumb_/','',$data);
echo $data;
$data1 = 'thumb_8_2393_Shades 1.jpg';
$data1 = preg_replace('/^thumb_/','',$data1);
echo $data1;
Output:-https://eval.in/606785
#RaimRaider give a very nice sugestion of using str_replace() in correct way like below:-
<?php
$data = 'hanger-cloth.jpg';
$data = substr($data,0,6)==='thumb_' ? str_replace( 'thumb_', '', $data ) : $data;
echo $data;
$data1 = 'thumb_8_2393_Shades 1.jpg';
$data1 = substr($data1,0,6)==='thumb_' ? str_replace( 'thumb_', '', $data1 ) : $data1;
echo $data1;
$filename=substr($filename,0,6)==='thumb_' ? str_replace( 'thumb_', '', $filename ) : $filename;
Output:-https://eval.in/606800
The solution using strpos and substr functions:
$images = ['thumb_8_2393_Shades 1.jpg','hanger-cloth.jpg', 'Red-Lehenga-1.jpg'];
foreach ($images as &$img) {
if (strpos($img, 'thumb_') === 0) { // if file name starts with 'thumb_'
$img = substr($img, 6);
}
}
print_r($images);
The output:
Array
(
[0] => 8_2393_Shades 1.jpg
[1] => hanger-cloth.jpg
[2] => Red-Lehenga-1.jpg
)
I have thousands of urls which have ids i want to get only ids from url for example
This is my array
Array
(
[0] => http://www.videoweed.es/file/f62f2bc536bad
[1] => http://www.movshare.net/video/5966fcb2605b9
[2] => http://www.nowvideo.sx/video/524aaacbd6614
[3] => http://vodlocker.com/pbz4sr6elxmo
)
I want ids from above links
f62f2bc536bad
5966fcb2605b9
524aaacbd6614
pbz4sr6elxmo
I have use parse_url function but its return me path which include all things after slash(/) like /file/pbz4sr6elxmo
<?php
foreach($alllinks as $url){
$parse = parse_url($url);
echo $parse['path'];
}
?>
Output
/pbz4sr6elxmo
/video/5966fcb2605b9
/file/f62f2bc536bad
/video/524aaacbd6614
You can try with explode -
$alllinks = array
(
'http://www.videoweed.es/file/f62f2bc536bad',
'http://www.movshare.net/video/5966fcb2605b9',
'http://www.nowvideo.sx/video/524aaacbd6614',
'http://vodlocker.com/pbz4sr6elxmo'
);
foreach($alllinks as $url){
$temp = explode('/', $url);
echo $temp[count($temp) - 1].'<br/>';
}
Output
f62f2bc536bad
5966fcb2605b9
524aaacbd6614
pbz4sr6elxmo
This will only help if the the url structure is same, i.e. the last part is the id
If the URLs always ends with the id you can simply do
$url = 'http://www.videoweed.es/file/f62f2bc536bad';
$url_split = explode('/', $url);
$code = $url_split[count($url_split) - 1];
Try this:
$alllinks = array(
'http://www.videoweed.es/file/f62f2bc536bad',
'http://www.movshare.net/video/5966fcb2605b9',
'http://www.nowvideo.sx/video/524aaacbd6614',
'http://vodlocker.com/pbz4sr6elxmo'
);
foreach($alllinks as $url){
$parts = explode('/', $url);
echo end($parts).'<br/>';
}
I have to extract a string like this:
index.php?module=Reports&action=abc&rname=Instantpayment
Now my task is to extract report, action and rname value in PHP.
I have tried by using explode(), but I am not able to extract module.
How can I do it?
You could use parse_str() in this case:
$string = 'index.php?module=Reports&action=abc&rname=Instantpayment';
$string = substr($string, strpos($string, '?')+1); // get the string from after the question mark until end of string
parse_str($string, $data); // use this function, stress free
echo '<pre>';
print_r($data);
Should output:
Array
(
[module] => Reports
[action] => abc
[rname] => Instantpayment
)
$yourUrl="module=Reports&action=abc&rname=Instantpayment"
$exploded_array = array();
parse_str($yourUrl, $exploded_array);
$exploded_array['module'];
$exploded_array['action'];
$exploded_array['rname'];
Use $_GET to get query strings from the URL
echo $_GET['module']; //Reports
echo $_GET['action']; // abc
echo $_GET['rname']; // Instantpayment
For getting from the string try explode():
$str ='index.php?module=Reports&action=abc&rname=Instantpayment';
$e = explode('?', $str);
$e1 = explode('&', $e[1]);
foreach($e1 as $v) {
$ex = explode('=', $v);
$newarr[$ex[0]] = $ex[1];
}
print_r($newarr); // Use this array of values you want.
//Array ( [module] => Reports [action] => abc [rname] => Instantpayment )
echo $newarr['module'];
echo $newarr['action'];
echo $newarr['rname'];
You have to access the globale GET variable:
$_GET['module']
$_GET['action']
$_GET['rname']
Try this:
<?php
$temp = "index.php?module=Reports&action=abc&rname=Instantpayment";
$t1 = explode("=",$temp);
for ($i = 1; $i < sizeof($t1); $i++)
{
$temp = explode("&", $t1[$i]);
echo $temp[0] . "\n";
}
?>
Question
i have string like this $str="a|apple||b|bat||c|cat||d|dog";
from the above string i want to create a array dynamically n that newly created array shud look like this Array
(
[a] => apple
[b] => bat
[c] => cat
[d] => dog
)
Question
i have html string like this
$html_string="<div>Content Div1</div>
<div>Content Div2</div>
<div>Content Div3</div>";
how can i get 3rd DIV ,resulting answer should be like this
$ans="<div>Content Div3</div>" ;
Please anyone help me
for the first one
$str = "a|apple||b|bat||c|cat||d|dog";
$new_array = array();
$my_array = explode("||", $str);
$my_array = array_filter($my_array);
foreach ($my_array as $mine) {
$my = explode("|", $mine);
$new_array[$my[0]] = $my[1];
}
print_r($new_array);
// Output
Array
(
[a] => apple
[b] => bat
[c] => cat
[d] => dog
)
**for second**
$html_string = "<div>Content Div1</div><div>Content Div2</div><div>Content Div3</div>";
$new_arr = explode("</div>", $html_string);
$my_data = $new_arr[2] . '</div>';
print_r($my_data);
// Output
<div>Content Div3</div>
Try this:
First
$str = "a|apple||b|bat||c|cat||d|dog";
$my_array = explode("||", $str);
$finalArr=array();
foreach($my_array as $my_arr)
{
$myar = explode("|", $my_arr);
$finalArr[$myar[0]]=$myar[1];
}
print_r($finalArr);
For Second
$html_string="<div>Content Div1</div><div>Content Div2</div><div>Content Div3</div>";
$secondArray = explode('</div>', $html_string);
echo $res = $secondArray[2] . "</div>";
Test it on http://writecodeonline.com/php/
try this:
1st Answer:
<?php
$str="a|apple||b|bat||c|cat||d|dog";
$parentArray = explode('||', $str);
$finalArray = array();
foreach($parentArray as $parentKey=>$parentValue)
{
$childArray = explode('|', $parentValue);
$finalArray[$childArray[0]] = $childArray[1];
}
echo "<pre>";
print_r($finalArray);
?>
2nd Answer
<?php
$html_string="<div>Content Div1</div>
<div>Content Div2</div>
<div>Content Div3</div>";
$finalArray = explode('</div>', $html_string);
$resultRequired = $finalArray[2] . "</div>";
?>
For your first question:
$tmp_array = explode( '||', $str );
$str_array = array();
foreach( $tmp_array as $value ){
$tmp = explode( '|', $value );
$str_array[ $tmp[0] ] = $tmp[1];
}
For your second question
$html_array = array();
$pattern = '/\<div\>.*\<\/div\>/i';
if( preg_match_all( $pattern, $html_string, $matches ) ) {
$html_array = $matches[0];
}
Which will make:
<div>Content Div3</div>
Be in $html_array[2] if any matches are found.
I have following records in text file, need to extract that record form text file and treat them as seperate array variables
r1=(1,2,3)|r2=(4,5,6)|r3=(1,2,3,4,5,7)|rn=(9,6,7,8) seperated by pipe(|)
I need to represent that as array use seperately like below
$r1= Array
(
[0] => 1
[1] => 2
[2] => 3
)
$r2=Array
(
[0] => 4
[1] => 5
[2] => 6
)
I have no idea how to do it, is it possible in php?
Just a plain regular expression to break up the string, followed by an explode on each group:
if (preg_match_all('#(\w+)=\(([\d,]*)\)#', $s, $matches)) {
foreach ($matches[2] as $i => $groups) {
$group_name = $matches[1][$i];
$$group_name = array_map('intval', explode(',', $groups));
}
}
print_r($r1);
print_r($r3);
print_r($rn);
You can use Eval
//Assuming you can pull the content from text file using fread
$temp = "r1=(1,2,3)|r2=(4,5,6)";
$temp=str_replace("=","=array",$temp);
$split=explode("|",$temp);
echo "<pre>";
foreach($split as $k=>$v){
$v="$".$v.";";
//Evaluate a string as PHP code .i.e You will get r1,r2 as a variable now which is array
eval($v);
}
print_r($r1);
print_r($r2);
$data = "r1=(1,2,3)|r2=(4,5,6)|r3=(1,2,3,4,5,7)|rn=(9,6,7,8)";
$arr = explode("|", $data);
$finArray = array();
foreach($arr as $key=>$value)
{
$single = explode('(', $value);
$finArray[] = explode(',', str_replace(')', '', $single[1]));
}
print_r($finArray);
can be done as:
$string="r1=(1,2,3)|r2=(4,5,6)|r3=(1,2,3,4,5,7)|rn=(9,6,7,8)";
$string=str_repla("r1=","",$string);
$yourArray=explode('|', $string);
This code will help you:--
<?php
$file = "/tmp/file1.txt"; // this is your file path
$f = fopen($file, "r");
while ( $line = fgets($f, 1000) ) {
print $line;
$a=explode('|',$line);
print_r($a); // I have explode based on | for you...
foreach($a as $key=>$value)
{
print_r($value);
}
fclose($file);
}
?>
""or""
$a="r1=(1,2,3)|r2=(4,5,6)|r3=(1,2,3,4,5,7)|rn=(9,6,7,8)";
$a=explode('|',$a);
print_r($a);
<?php
$file = "file.txt";
$f = fopen($file, "r");
while ( $line = fgets($f, 1000) ) {
$str = $line;
}
$str1 = explode("|",$str);
foreach($str1 as $temp) {
$str2 = explode("=",$temp);
$data[$str2[0]] = explode(",",trim($str2[1],"()"));
}
echo '<pre>';
print_r($data);
echo '</pre>';
?>
This will do your job.