Short String after php explode - php

HI I get a string in our data base after completing a quiz which contains the results of each question in a single string. like:
&q1=2&q2=5&q14=7&q6=8&q4=9&q19=10&q12=1&q20=3
each question can be skipped for a while to answer later, so the answers are unsorted.
now I want the results like
q1=2,q2=5,q4=9,q6=8,q12=1,q14=7,q19=10,q20=3
Can anyone help me.?

Try this
$a='&q1=2&q2=5&q14=7&q6=8&q4=9&q19=10&q12=1&q20=3';
$b=explode('&',$a);
natsort($b);
$c=implode(',',$b);
print($c);

try like this
$a='&q1=2&q2=5&q14=7&q6=8&q4=9&q19=10&q12=1&q20=3';
$a = ltrim($a,'&');
$b=explode('&',$a);
natsort($b);
echo $c=implode(',',$b);

try this one:
$url = '&q1=2&q2=5&q14=7&q6=8&q4=9&q19=10&q12=1&q20=3';
parse_str($url, $urlDecoded);
$urlDecoded = array_flip($urlDecoded);
natsort($urlDecoded);
$urlDecoded = array_flip($urlDecoded);
var_dump($urlDecoded);

Other examples were fine but they all had a leading comma ,.
Here is an improved version:
$a=explode('&','&q1=2&q2=5&q14=7&q6=8&q4=9&q19=10&q12=1&q20=3');
natsort($a);
echo substr(implode(',',$a),1);

Related

PHP replace string does not work

In my db a have this message MSG01:
"Availability between #DATA_MIN# - #DATA_MIN#"
Query:
select * from messages where MSG_CODE = "MSG01"
Column format is VARCHAR(500).
I have this code to replace #DATA_MIN# and #DATA-MAX#:
$date = array($date->data_min, $date->data_max);
$replace_string = array("#DATA_MIN#", "#DATA_MAX#");
$text= str_replace($replace_string,$date, lang("MSG01")).
But on my site it appears like this: Availability between #DATA_MIN# - #DATA_MIN#. Why does it not replace the values?
You have one of two problems:
Your site is not using $text from this example.
Your $date->data_min property has the value "#DATA_MIN#".
I've solved the problem. I've removed the # from DATA_MIN - DATA_MAX. It's working now.
Thank you, guys!

how to split a URL for get the id php

i have this URL:
www.blublub.de/niedersachsen.html?veranstaltung=Neumarkt+Osnabrück~W011Cb5ETzN2EJoG9N~10.04.2014
And i like to get the id-number **"W011Cb5ETzN2EJoG9N"** from the URL.
How can i implement this in php ?
please help me.
$parts=explode('~', $_GET['veranstaltung']);
$id=$parts[1];
try this
<?php
$veranstaltung=$_GET['veranstaltung'];
$str=explode('~',$veranstaltung);
$idnumber=$str[1];
echo $idnumber;
?>
get and explode it
try to get and explode with (~) your url query string
$url = $_GET['veranstaltung'];
$myid = explode('~', $url);
echo $myid[1]; // output - W011Cb5ETzN2EJoG9N
Use regular exp for this
if (preg_match_all("/~(.*?)~/",$_GET['veranstaltung'],$result){
print_R($result)
}

How to trim the string from a character to the next immediate occurring of another character

http://zyx.com/abc.html?style=1876&price=2%2C1000&size=68
http://zyx.com/abc.html?price=2%2C1000&style=1876&size=68
The url can appear in any of the two form:
I want to remove the entire price=2%2C1000& from my url.
I tried this thread. But with no luck
How to do this?
Try like this :
$str = explode("price","http://zyx.com/abc.html?style=1876&price=1%2C1000%2C2%2C1000&size=68");
$removeable_str = explode("&", $str[1]);
unset($removeable_str[0]);
echo $str[0].join("&",$removeable_str);
Try that:
var s = "http://zyx.com/abc.html?style=1876&price=2%2C1000&size=68"
s.split("price=")[1].split("&")[0];

PHP preg_replace problem

This is a follow-up question to the one I posted here (thanks to mario)
Ok, so I have a preg_replace statement to replace a url string with sometext, insert a value from a query string (using $_GET["size"]) and insert a value from a associative array (using $fruitArray["$1"] back reference.)
Input url string would be:
http://mysite.com/script.php?fruit=apple
Output string should be:
http://mysite.com/small/sometext/green/
The PHP I have is as follows:
$result = preg_replace('|http://www.mysite.com/script.php\?fruit=([a-zA-Z0-9_-]*)|e', ' "http://www.mysite.com/" .$_GET["size"]. "/sometext/" .$fruitArray["$1"]. "/"', $result);
This codes outputs the following string:
http://mysite.com/small/sometext//
The code seems to skip the value in $fruitArray["$1"].
What am I missing?
Thanks!
Well, weird thing.
Your code work's perfectly fine for me (see below code that I used for testing locally).
I did however fix 2 things with your regex:
Don't use | as a delimiter, it has meaning in regex.
Your regular expression is only giving the illusion that it works as you're not escaping the .s. It would actually match http://www#mysite%com/script*php?fruit=apple too.
Test script:
$fruitArray = array('apple' => 'green');
$_GET = array('size' => 'small');
$result = 'http://www.mysite.com/script.php?fruit=apple';
$result = preg_replace('#http://www\.mysite\.com/script\.php\?fruit=([a-zA-Z0-9_-]*)#e', ' "http://www.mysite.com/" .$_GET["size"]. "/sometext/" .$fruitArray["$1"]. "/"', $result);
echo $result;
Output:
Rudis-Mac-Pro:~ rudi$ php tmp.php
http://www.mysite.com/small/sometext/green/
The only thing this leads me to think is that $fruitArray is not setup correctly for you.
By the way, I think this may be more appropriate, as it will give you more flexibility in the future, better syntax highlighting and make more sense than using the e modifier for the evil() function to be internally called by PHP ;-) It's also a lot cleaner to read, IMO.
$result = preg_replace_callback('#http://www\.mysite\.com/script\.php\?fruit=([a-zA-Z0-9_-]*)#', function($matches) {
global $fruitArray;
return 'http://www.mysite.com/' . $_GET['size'] . '/sometext/' . $fruitArray[$matches[1]] . '/';
}, $result);
i write it again, i don't understand good where is the error, the evaluation of preg results is very weird in php
preg_replace(
'|http\://([\w\.-]+?)/script\.php\?fruit=([\w_-]+)|e'
, '"http://www.$1/".$_GET["size"]."/sometext/".$fruitArray["$2"]."/";'
, $result
);
It looks like you have forgotten to escape the ?. It should be /script.php\?, with a \? to escape properly, as in the linked answer you provided.
$fruitArray["\$1"] instead of $fruitArray["$1"]

fetching the url in php

my url is like below:
http://www.xyz.org/abc/list.php?id=1
now "$_SERVER['PHP_SELF']" gives me only "abc/list.php" this portion
and "$_SERVER['REQUEST_URI']" gives me "abc/list.php?id=1" this
now if i want to fetch only the portion "?id=1" how to do that.
coz im having problems in the paging query for this.
thanxx in advance...
It's $_SERVER['QUERY_STRING'].
Use $_GET['id'] to access the id parameter.
Take a look at http://php.net/parse_str, http://php.net/parse-url and http://php.net/http_build_query
Depending on what you're doing http://bradym.net/php/modify-query-string-parameters may also be helpful.
$_GET['id'] or $_REQUEST['id']
if your not sure which GET values u'll receive u also could
$fullRequest = exlode('?', $_SERVER['REQUEST_URI']);
$params = array();
foreach(explode('&', $fullRequest) as $part){
foreach(explode('=', $part) as $keyVal){
$params[$keyVal[0]] = $keyVal[1];
}
}

Categories