Get specific part of a URL value - php

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

Related

Changing specific text in 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);

Get content between shortcodes

I need to get the content between a wordpress shortcode.
Like say [bla_blabla name="a"]Variation A[/bla_blabla]
should return Variation A
I thought of using regex to get it, but there is a shortcodes.php file in wordpress which should already be capable of doing it? Could you guide me into the correct way of doing it?
I have currently used this code
<?php
function abtest_runner($input) {
//A bit confused if you wanted equal chances of selecting one or equal changes?
//This is for equal chances.
$size = count($input);
$select = rand(0,$size-1);
$temp=array();
$selection = $input[$select];
$temp = (explode("]",$selection));
$temp = (explode("[",$temp[1]));
echo $temp[0];
}
$input[0] = '[bla_blabla name="a"]Variation A[/bla_blabla]';
$input[1] = '[bla_blabla name="b"]Variation B [/bla_blabla]';
abtest_runner($input);
?>
If you have registered you shortcode in WordPress using the [add_shortcode][1] function then, you can extract your shortcode content using the following method:
$pattern = get_shortcode_regex();
$content = '[bla_blabla name="a"]Variation A[/bla_blabla]';
$matches = array();
preg_match("/$pattern/s", $content, $matches);
print_r($matches[5]);
$matches[5] will contain your content.
Hope this helps.

Retrieve only the category ID from URL

I have my URL structure as www.site.com/listings/232-real-estate where 232 is the category ID and real-estate as my category name.
I want to retrieve only the numeric ID, to be used to extract data from the database using the category ID. How do I do that?
You can use something as simple as:
<?php
preg_match_all('#(\d+)#', $url, $matches);
$catid = $matches[0];
?>
But this does not have adequate checks in place. Ideally, you want to split on basis of /:
<?php
$elements = explode('/',$url);
$category = $elements[2];
$subelements = explode('-',$category);
$categoryid = intval($subelements[0]);
?>
You can use preg_replace:
id = preg_replace('/^.*\/([0-9]*)-[^\/]*$/','$1')
This assumes that the last / in the url is just before the category ID.
If 232-real-estate is stored in the variable $var you can get the ID using:
$id = strtok($var, '-');
Incidentally, it is then trivial to get the name:
$name = strtok(''); // returns: estate-agent

Get second argument in php navigation

I am trying to grab the second argument of a URL such as this:
http://website.com/?p=2?id=ass92jdskdj92
I just want the id portion of the address. I use the following code to grab the first portion of the address:
$p = $_GET[p];
$remove = strrchr($p, '?');
$p = str_replace($remove, "", $p);
Any hints on how to get the second portion?
Arguments in links are started by ? and divided by &.
That means your link should look like this:
http://website.com/?p=2&id=ass92jdskdj92
And you get them by:
$p = $_GET['p'];
$id = $_GET['id'];
First change the URL
only first argument start with ? and rest of all is append by &
u should try with
echo parse_url($url, PHP_URL_PATH);
Reference:
parse url

Grab the video ID only from youtube's URLs

How can I grab the video ID only from the youtube's URLs?
For instance,
http://www.youtube.com/watch?v=aPm3QVKlBJg
sometime the URLs contain other information after the 'v' like
http://www.youtube.com/watch?v=Z29MkJdMKqs&feature=grec_index
but I don't want the other info, just video ID.
I only can think of using explode,
$url = "http://www.youtube.com/watch?v=aPm3QVKlBJg";
$pieces = explode("v=", $url);
but how to clean up the URLs like this?
http://www.youtube.com/watch?v=Z29MkJdMKqs&feature=grec_index
You should never use regular expressions when the same thing can be accomplished through purpose-built functions.
You can use parse_url to break the URL up into its segments, and parse_str to break the query string portion into a key/value array:
$url = 'http://www.youtube.com/watch?v=Z29MkJdMKqs&feature=grec_index'
// break the URL into its components
$parts = parse_url($url);
// $parts['query'] contains the query string: 'v=Z29MkJdMKqs&feature=grec_index'
// parse variables into key=>value array
$query = array();
parse_str($parts['query'], $query);
echo $query['v']; // Z29MkJdMKqs
echo $query['feature'] // grec_index
The alternate form of parse_str extracts variables into the current scope. You could build this into a function to find and return the v parameter:
// Returns null if video id doesn't exist in URL
function get_video_id($url) {
$parts = parse_url($url);
// Make sure $url had a query string
if (!array_key_exists('query', $parts))
return null;
parse_str($parts['query']);
// Return the 'v' parameter if it existed
return isset($v) ? $v : null;
}

Categories