How to explode vaules in $_GET['']=1#5#6 in php? - php

Hi in my url i have variables stored in ../index.php?cat=1#5#8 in so on how to separate them using explode function so out put could be
arr[0]=1
arr[1]=5
arr[2]=8

Try with explode like
$arr = explode("#",$_GET['cat']);
But as #Bora said after # the remaining string will may not be sent ,so better to use '_' in place of '#' (1_5_8_....)and can explode it like
$arr = explode("_",$_GET['cat']);

Try this:
$array=$_GET['cat'];
$result = explode("#",$array);

Related

Exploding a URL

What am I doing wrong? I currently have the following code below and I am trying to just get the secondary url which links to the direct MP3 path.
$date = $html2->find('strong > a',0);
$explode = explode ("http://www.example.com/?dl_name=", $date->href);
print $explode;
It returns ARRAY.
explode returns an array of strings, try using print_r($explode) and then use $explode[0], $explode[1]...
Could you resolve this with str_replace:
$secondary_url = str_replace("http://www.example.com/?dl_name=", '', $date->href);
You will get the rest of this string in $secondary_url.
Reference: str_replace

Convert URL GET var to another variable

I have a URL variable that looks like this:
http://myURL?price_rang=2%5E%5E99_5%5E%5E99
With php echo on the price_rang GET var it translates to 25E5E99_55E5E99 .
I need it to be converted to 2.99_5.99 and then explode it and use the first and second part in a query.
What would be the best way to convert this string and explode it (or just convert it)?
Take it step by step.
get the info
replace your string
explode
Try the following:
$range = $_GET['price_rang'];
$range = str_replace("^^", ".", $range);
$val = explode("_", $range);
var_dump($val);
You need rawurldecode() or urldecode() function
$val = explode('_', rawurldecode($_GET['price_rang']));

How to convert string variable to an array in php?

I have one string variable as
$p_list="1,2,3,4";
i want to convert it to an array such as
$a[0]='1';
$a[1]='2';
$a[2]='3';
$a[3]='4';
how to do this in php?
Use explode.
$p_list = "1,2,3,4";
$array = explode(',', $p_list);
See Codepad.
Try explode $a=explode(",","1,2,3,4");
Try this:
In PHP, explode function will convert string to array, If string has certain pattern.
<?php
$p_list = "1,2,3,4";
$noArr = explode(",", $p_list);
var_dump($noArr);
?>
You will get array with values stores in it.
Thanks

String at the end of a text inside another string

I would like to use one string inside another string, like this:
$url = "http://www.sample.com/index.php?nr=$string[0]";
how should I insert this string at the end?
$url = "http://www.sample.com/index.php?nr={$string[0]}";
or
$url = "http://www.sample.com/index.php?nr=" . $string[0];
just like your example
$url = "http://www.sample.com/index.php?nr=$string[0]";
As a solution to your problem please try executing following code snippet.
You need to concatenate strings in php using dot operator(.)
$url = "http://www.sample.com/index.php?nr=".$string[0];

How to insert an additional path into URL using PHP?

Let say I've this URL:
http://example.com/image-title/987654/
I want to insert "download" to the part between "image-title" and "987654" so it would look like:
http://example.com/image-title/download/987654/
help would be greatly appreciated! thank you.
Assuming your URIs will always be the same (or at least predictable) format, you can use the explode function to split the URI into each of its parts, and then use array_splice to insert elements into that array, and finally use implode to put it all back together into a single string.
Note that you can insert elements into an array by specifying the $length parameter as zero. For example:
$myArray = array("the", "quick", "fox");
array_splice($myArray, 2, 0, "brown");
// $myArray now equals array("the", "quick", "brown", "fox");
There are a number of ways to do this in PHP:
Split and reconstruct using explode(), array_merge, implode()
Using substring()
Using a regular expression
Using str_replace
Assuming all the url's conform to the same structure (image-title/[image_id]) i recommend using str_replace like so:
$url = str_replace('image-title', 'image-title/download', $url);
If however image-title is dynamic (the actual title of the image) i recommend splitting and reconstructing like so:
$urlParts = explode('/', $url);
$urlParts = array_merge(array_slice($urlParts, 0, 3), (array)'download', array_slice($urlParts, 3));
$url = implode('/', $urlParts);
Not very well formatted, but i think this is what you need
$mystr= 'download';
$str = 'http://example.com/image-title/987654/';
$newstr = explode( "http://example.com/image-title",$str);
$constring = $mystr.$newstr[1];
$adding = 'http://example.com/image-title/';
echo $adding.$constring; // output-- http://example.com/image-title/download/987654/

Categories