Changing specific text in php - php

$url = localhost/project/index.php?letter=0&position=0&bypass=1
How to change position=0 to position=1?
The new $url value will be:
$url = localhost/project/index.php?letter=0&position=1&bypass=1

You can use parse-str and parse-url approach with the help of http-build-query,
$url = "localhost/project/index.php?letter=0&position=0&bypass=1";
// fetching query paramters and save it to output variable
parse_str(parse_url($url,PHP_URL_QUERY),$output);
// changing position value
$output["position"] = 1;
// building back query string
$query = http_build_query($output);
// creating final string
echo parse_url($url,PHP_URL_PATH)."?".$query;
Demo
Output:-
localhost/project/index.php?letter=0&position=1&bypass=1

You have to use the str_replace() function to replace a specific text from string.
$url = str_replace('position=0','position=1',$url);

Related

Replace url paramer by using php

I have a url & its structure like
https://www.example.com/i/location-name/category/subcategory/item/item-id
I want to replace the url parameter(location-name) dynamically by new parameter using PHP.
Modified url is looks like this
https://www.example.com/i/new-parameter/category/subcategory/item/item-id
I had successfully done with query parameters by using http_build_query();
But in this case i had tried with preg_replace(), but its not working
Thanks
If you would like to use regex.
$url = 'https://www.example.com/i/location-name/category/subcategory/item/item-id';
$new_param = 'new-parameter';
print preg_replace('|/location-name/|','/'.$new_param.'/',$url);
I don't recommend search for only location-name (without slash) beacause it will match with for example location-names string.
UPDATE
Based on placement not string you can change that part that way:
$url = 'https://www.example.com/i/location-name/category/subcategory/item/item-id';
$new_param = 'new-parameter';
$new_url = preg_replace('|/i/(.*?)/|','/i/'.$new_param.'/',$url);
print $new_url.'<br/>';
$new_param = 'another-parameter';
$new_url = preg_replace('|/i/(.*?)/|','/i/'.$new_param.'/',$url);
print $new_url.'<br/>';
You will get:
https://www.example.com/i/new-parameter/category/subcategory/item/item-id
https://www.example.com/i/another-parameter/category/subcategory/item/item-id
Alternative solution
If you would like to be sure about change, you can do it another way, something like this:
$url = 'https://www.example.com/i/location-name/category/subcategory/item/item-id';
$new_param = 'new-param';
$parts = parse_url($url);
$path_parts = explode('/',$parts['path']);
$path_parts[2] = $new_param;
$new_path = implode('/',$path_parts);
$new_url = $parts['scheme'].'://'.$parts['host'].$new_path;
print( $new_url);
Try this one:
$new_url = str_replace('location-name', 'my-location', 'https://www.example.com/i/location-name/category/subcategory/item/item-id')

strstr with strpos not working (PHP)

I am trying to trim the end of my URL after certain characters.
I have my URL in a SESSION variable.
if(strpos($_SESSION['LP'],'ordrer'))
{
$Order1 = $_SESSION['LP'];
$Order2 = strpos($Order1, "ordrer");
$Order = substr($Order1,0,$Order2);
//$Order1 = $Order1(explode('ordrer', $Order1);
}
I'm first putting my URL in variable. Then looking if there is "ordrer" in it with strpos and then cut the end of my URL after "ordrer".
Bassicly I have /Dossiers.php?ordrer=Numero or /Dossiers.php?ordrer=Status and I want to have /Dossiers.php?
But I does not do anything to the URL, why is that?
Also tried explode(), same result.
Thanks
I can't see anything wrong with your code - $Order should contain your URL. I've tested the following code where I put your URL into $url. Could it be that $_SESSION['LP'] is empty?
I've also created a second version at the bottom for you, using explode.
$url = "/Dossiers.php?ordrer=Numero";
//Your version
if (strpos($url,'ordrer'))
{
$Order1 = $url;
$Order2 = strpos($Order1, "ordrer");
$Order = substr($Order1,0,$Order2);
echo $Order; // the first part, including ?
}
//If there is no question mark, you'll just get the whole string
$parts = explode("?", $url);
$Order = $parts[0];
echo $Order; //The first part, excluding ?
Edit: Additional thought - is your problem that "ordrer" should be "order" ?

PHP: str_replace within a word/phrase

I am trying to set up a small script that can play youtube videos but thats kinda besides the point.
I have $ytlink which equals www.youtube.com/watch?v=3WAOxKOmR90
But I want to make it become www.youtube.com/embed/3WAOxKOmR90
Currently I have tried
$result = str_replace('https://youtube.com/watch?v=', "https://youtube.com/watch?v=", $ytlink);
But this returns it as standard
I have also tried
preg_replace('/https://youtube.com/watch?v=/, '/https://youtube.com/embed/', $ytlink);
but both of these dont work.
Instead of using ugly regexes, I recommend using parse_url() with parse_str(). This allows you to be flexible in the event that you want to change something or if Youtube decides to change their URL slightly.
$url = 'https://www.youtube.com/watch?v=3WAOxKOmR90';
// Parse the URL into parts
$parsed_url = parse_url($url);
// Get the whole query string
$query = $parsed_url['query'];
// Parse the query string into parts
parse_str($query, $params);
// Get the parameter you want
$v = $params['v'];
// Now re-build the URL how you want
echo $parsed_url['scheme'].'://'.$parsed_url['host'].'/embed/'.$v;
// Outputs: https://www.youtube.com/embed/3WAOxKOmR90
This works:
$ytlink = 'www.youtube.com/watch?v=3WAOxKOmR90';
$result = str_replace('watch?v=', 'embed/', $ytlink);
echo $result;
$url = 'www.youtube.com/watch?v=3WAOxKOmR90';
echo preg_replace('/.*?v=(\w+)/i', 'www.youtube.com/embed/$1', $url);

Get specific part of a URL value

I used a code to bulk import the envato items,
for example the envato items url is like this :
http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226
How to get only the part "2833226" of this URL with PHP?
I use wordpress and have used custom fields to insert envato item links
for example a custom field (afflink) with value :
http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226
and this code to call the custom field value in the theme
$afflink = get_post_meta($post->ID, "afflink", false);
How to get only the item number?
Use explode to split using / caracter and then get the last element of array:
<?php
$url='http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226';
$y=explode('/',$url);
$affiliateID = end($y);
?>
$pattern = "/\d+$/";
$input = "http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226";
preg_match($pattern, $input, $matches);
//Your ID
$post_id = $matches[0];
Use this :
value = window.location.href.substring(window.location.href.lastIndexOf('/') + 1);
$url = 'http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226?r=1';
$urlParts = parse_url($url);
$path = $urlParts['path'];
$pathParts = explode('/',$path);
$item_id = end($pathParts);
The above code will make sure to avoid query string and read only Uri

Php replace in string

I've got string based on current url, look like this:
domain.com/katalog_firm,p11.html?typ=lista&fraza=&search_group=1&search_type=1&kategoria=1&podkategoria=0&wojewodztwo=0&miejscowosc=0&page=1&limit=10
I want to do a pagination. How to replace in this strng page=x, and limit is choosen from dropdown so also limit=x
So I want to change exxample from above to
domain.com/katalog_firm,p11.html?typ=lista&fraza=&search_group=1&search_type=1&kategoria=1&podkategoria=0&wojewodztwo=0&miejscowosc=0&page=2&limit=25
I just want to make replacement from (page and limit)
domain.com/katalog_firm,p11.html?typ=lista&fraza=&search_group=1&search_type=1&kategoria=1&podkategoria=0&wojewodztwo=0&miejscowosc=0&page=1&limit=10
to
domain.com/katalog_firm,p11.html?typ=lista&fraza=&search_group=1&search_type=1&kategoria=1&podkategoria=0&wojewodztwo=0&miejscowosc=0&page=2&limit=25
where page will be variable from loop, and limit will be constant
You can break the string into an array then modify the required values and again create the string.
<?php
$url = "domain.com/katalog_firm,p11.html?typ=lista&fraza=&search_group=1&search_type=1&kategoria=1&podkategoria=0&wojewodztwo=0&miejscowosc=0&page=1&limit=10";
$params = explode("?",$url,2);
parse_str($params[1], $url_array);
$url_array['page'] = 2;
$url_array['limit'] = 5;
$newparams = http_build_query($url_array);
$newurl = $params[0]."?".$newparams;
?>

Categories