How do i replace symbols in url? [duplicate] - php

This question already has answers here:
URL rewriting with PHP
(5 answers)
Closed 5 years ago.
I am unsure if it's possible to simply do some replace inside php or if i should look at .htaccess for this? What would be the best solution to clean up this url as desired.
The reason why this is not a duplicate is because i dont have a static url, the id's and text will be changed depending on what post is clicked on the website.
i have tried to do this with .htaccess but it doesn't work for me since i fetch different titles and id's depending on what post users decide to watch. this is how my href looks like
<?php echo $row['postTitle'] ?>
I would like to know how i would change this url for example
www.website.com/index.php?page=viewpost&id=26&title=title%of%the%post
to
www.website.com/index.php?page=viewpost/26/title_of_the_post

you didn't really explain what you are trying to do but here's how to exactly what you ask for.
<?php
$url = "www.website.com/index.php?page=viewpost&id=26&title=title%of%the%post";
$parts = explode('?',$url);
$url = $parts[0] . '?page=' $parts[1] ;
unset($parts[0]);
unset($parts[1]);
foreach($parts as $key => $part) {
$url .= '/'.$part;
}
echo $url;
?>

Related

Get portion of URL with PHP [duplicate]

This question already has answers here:
Get parts of URL in PHP
(4 answers)
Closed 1 year ago.
I had previously been using basename to grab the last part of my URL however have noticed some issues if my URL contains parameters.
So if my URL is this:
https://www.google.com/test-page/?utm_source=test
How would I pull test-page from this?
You split it by the / delimiter, and then take the fourth item
$link = 'https://www.google.com/test-page/?utm_source=test';
$split = explode('/', $link);
if(isset($split[3]))
{
echo $split[3];
}

PHP Cut URL from some variable query [duplicate]

This question already has answers here:
Strip off specific parameter from URL's querystring
(22 answers)
Closed 4 years ago.
if I have some URL like these:
http://localhost/myfile.php?start=2018&end=2019&page=1&status=
Did anynone know how to replace any words from that link that contain page= ?
The result that I want is :
http://localhost/myfile.php?start=2018&end=2019&status=
Even if the link is like this:
http://localhost/myfile.php?start=2018&end=2019&status=&page=3
I still want the result will be without page variable and value, so it might be:
http://localhost/myfile.php?start=2018&end=2019&status=
any idea how to do this? Especially without regex (regex is my last option)
You can use parse_url(), parse_str() and http_build_query():
// Your URL
$str = 'http://localhost/myfile.php?start=2018&end=2019&page=1&status=';
// Get parts
$parts = parse_url($str);
// Get array of arguments
parse_str($parts['query'], $args);
// Remove unwanted index
unset($args['page']);
// Rebuild your URL
echo $parts['scheme'] . '://' . $parts['host'] . $parts['path'] . '?' . http_build_query($args);
// Output: http://localhost/myfile.php?start=2018&end=2019&status=
I encouraged to read documentation or to print_r() vars of this sample code for a better understanding.

Remove character from image url [duplicate]

This question already has answers here:
How to remove the querystring and get only the URL?
(16 answers)
Closed 8 years ago.
I'm new to PHP and WordPress, and i need to import image from a offshore website with the WordPress function download_URL().
My problem is, i have URL that look like that :
http://www.test/img/FMR.jpg
But i have also URL that look like that :
http://www.test/img/FMR.jpg?Qq_0y12h
And can i have remove everything that is after the .extension if there something?
you can use preg_replace, I did this little example for you.
<?php
$url = "http://www.test/img/FMR.jpg?Qq_0y12h";
echo "url = $url\n\n";
$urlFormatted = preg_replace("/\?.*$/", "", $url);
echo "urlFormatted = $urlFormatted\n";
?>

Extract site link from google URL [duplicate]

This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 8 years ago.
I want to extract site link from Google URL, I need an efficient way to do this,
I have extracted this, but i am not comfortable with that like,
$googleURL = "http://www.google.ca/local_url?dq=food+Toronto,+ON&q=https://plus.google.com/110334461338830338847/about%3Fgl%3DCA%26hl%3Den-CA&ved=0CHAQlQU&sa=X&ei=HzrCVNX-JqSzigb-94D4CQ&s=ANYYN7nQx_FiR1PuowDmXBi1oyfkI2MImg";
I want this
https://plus.google.com/110334461338830338847/
I have done this in a following way.
$first = current(explode("about", $googleURL)); // returns http://www.google.ca/local_url?dq=food+Toronto,+ON&q=https://plus.google.com/110334461338830338847/
and then,
$myLink = explode("&q=", $first);
echo $myLink[1]; // return my need => https://plus.google.com/110334461338830338847/
but there may be two "about" or "&q=" in a googleURL which can cause problem.
I know that, this googleURL will be redirected to my need, but I need that specific link for a purpose.
I guess that it is not really safe to parse that since google can change its implementation anytime.
However, if you want to get a parameter from a String url, this question covers it pretty well :
How to get parameters from a URL string?
$parts = parse_url($googleUrl);
parse_str($parts['query'], $query);
echo $query['q'];

PHP Regular expression - forget everything after? [duplicate]

This question already has answers here:
Beautiful way to remove GET-variables with PHP?
(12 answers)
How to remove content from url after question mark. preg_match or preg_replace?
(2 answers)
Closed 8 years ago.
I have this url: http:www.blabla.com/x/x/x/x?username=testuser
I need a string to read this url, but forget everything and including the ? mark.
So it becomes this: http:www.blabla.com/x/x/x/x
The reason for this is because I am making this variable:
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
And this code:
if($host == "http:www.blabla.com/x/x/x/x") {
echo "lul";
}
But right now, the URL changes depending on what user is on, and it has to execute the echo no matter what user is on.
So I read some reges and preg_match etc. and I just wanted to hear your opinions or advice. How would I accomblish this the best? thanks!
This is too trivial of a task for regex.
$host = $_SERVER['SERVER_NAME'] . explode("?", $_SERVER['REQUEST_URI'], 2)[0];
(Note: this assumes you're up-to-date, or at least using PHP 5.4, for the dereference to work without a temporary variable)
Or if you must omit the get / request section just explode ? and use $host[0]

Categories