0

I have some very long URL variables. Here is one example.

http://localhost/index.php?image=XYZ_1555025022.jpg&mppdf=yes&pdfname=Printer&deskew=yes&autocrop=yes&print=no&mode=color&printscalewidth100=&printscaleheight100=&rand=56039

Ultimately it would be nice if I could find a way to use preg_replace to simply change one variable even if in the middle of the string for instance in the string above change print=no to 'print=yes for example.

I will however settle for a preg_replace pattern match that allows me to delete ?image=XYZ_1555025022.jpg. as this is a variable the name could be anything. It will always have "?image" " at the start and end with "&"

I think one of the problems I have run into is that preg_match seems to have issues on strings with "=" contained in them .

I am completely lost here in this and all those characters make may head spin. Maybe someone can give some guidance please?

1
  • if you just want to replace exact string why not use str_replace? Commented Apr 18, 2019 at 2:44

2 Answers 2

1

Here's a demo of how you can do some of things you want using explode, parse_str and http_build_query:

$url = 'http://localhost/index.php?image=XYZ_1555025022.jpg&mppdf=yes&pdfname=Printer&deskew=yes&autocrop=yes&print=no&mode=color&printscalewidth100=&printscaleheight100=&rand=56039';
// split on first ?
list($path, $query_string) = explode('?', $url, 2);
// parse the query string
parse_str($query_string, $params);
// delete image param
unset($params['image']);
// change the print param
$params['print'] = 'yes';
// rebuild the query
$query_string = http_build_query($params);
// reassemble the URL
$url = $path . '?' . $query_string;
echo $url;

Output:

http://localhost/index.php?mppdf=yes&pdfname=Printer&deskew=yes&autocrop=yes&print=yes&mode=color&printscalewidth100=&printscaleheight100=&rand=56039

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

4 Comments

@NullPoiиteя it couldn't (it would be URL encoded). But how can you write a str_replace which will work for any filename between image= and &?
agreed, image can have = , my bad, anyway only member for 1year and 40k nice work sir
@NullPoiиteя thanks - I have to confess to have become a bit addicted to that little green pop-up :-)
@Nick absolutely AMAZING . I can not tell you how tired I was getting of tracking these URL variables and what applies where and typing '&this='.$that. WHat a relief. I looked for an answer like this a year ago to no avail but nw back to the same code and you made it so much easiier. Thank you!
1

You can use str_replace() or preg_replace() to get your job done, but parse_url() with parse_str() will give you more controls to modify any parameters easily by array index. Finally use http_build_query() to make your final url after modification.

<?php
$url = 'http://localhost/index.php?image=XYZ_1555025022.jpg&mppdf=yes&pdfname=Printer&deskew=yes&autocrop=yes&print=no&mode=color&printscalewidth100=&printscaleheight100=&rand=56039';
$parts = parse_url($url);
parse_str($parts['query'], $query);
echo "BEFORE".PHP_EOL;
print_r($query);
$query['print'] = 'yes';
echo "AFTER".PHP_EOL;
print_r($query);
?>

DEMO: https://3v4l.org/npGij

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.