basically i just want to get all the text after the _ .
i have tried
$productid = split("_",$PagePath, 1);
with no success what is the correct way of doing this?
Use explode instead of split. The result of explode is an array, so use list():
list(,$productid) = explode('_', $PagePath, 2);
Note the third parameter, 2 instead of 1. Using 1 will not split anything. Or, just use preg_replace:
$productid = preg_replace('/^.*?_/', '', $PagePath);
$productId = substr($string, (strpos($PagePath, '_') + 1)); //+1 accounts for the underscore
You could try:
$productidtokens = explode("_",$pagePath, 2);
if(count($productidtokens)>1)
$productid = $productidtokens[1];
Try $productid = explode('_',$PagePath).
If you just intended to take the text after the '_', then get rid the first index in the array created by write : array_shift($productid).
Related
Substr PHP
I have a string like http://domain.sf/app_local.php/foo/bar/33.
The last characters are the id of an element. Its length could be more than one, so I can not use:
substr($dynamicstring, -1);
In this case, it must be:
substr($dynamicstring, -2);
How can I get the characters after "/bar/" on the string without depending on the length?
To ensure you are getting an immediate section after the bar, use regular expressions:
preg_match('~/bar/([^/?&#]+)~', $url, $matches);
echo $matches[1]; // 33
You can use explode(), like this:
$id = explode('/',$var);
And take the element where you had the id.
You could use explode('/', $dynamicstring) to split the string into an array of the strings inbetween each /. Then you could use end() on the result of this to get the last part.
$id = end(explode('/', $dynamicstring));
Try this:
$dynamicstring = 'http://domain.sf/app_local.php/foo/bar/33';
// split your string into an array with /
$parts = explode('/', $dynamicstring);
// move the array pointer to the end
end($parts);
// return the current position/value of the $parts array
$id = current($parts);
// reset the array pointer to the beginning => 0
// if you want to do any further handling
reset($parts);
echo $id;
// $id => 33
Test it yourself here.
You can use a regular expression to do it:
$dynamicstring = "http://domain.sf/app_local.php/foo/bar/33";
if (preg_match('#/([0-9]+)$#', $dynamicstring, $m)) {
echo $m[1];
}
I tested it out myself before answering. Other answers are reasonable too, but this will work according to your need...
<?php
$url = "http://domain.sf/app_local.php/foo/bar/33";
$id = substr($url, strpos($url, "/bar/") + 5);
echo $id;
Please find the below answer.
$str = "http://domain.sf/app_local.php/foo/bar/33";
$splitArr = explode('/', explode('//', $str)[1]);
var_dump($splitArr[count($splitArr)-1]);
i have a link with the following (varying) structure:
http://website.com/category/product/abc12345
the category- and the productname vary concerning their length, but the id (abc12345) is always located after the last "/" and is about 6-8 chars long.
i tried to extract the link with the following code:
preg_split('/[/]/', $val, $arr);
$narr[] = end($arr);
with val being the link and arr being the array, the result should be pasted in.
narr is the array i want to save to a file later, so it doesn't really matter here. unfortunately, my results are always empty when i try it this way.
I would go for:
$urlParts = parse_url($myUrl);
$parts = explode('/', $urlParts['path']);
$productId = array_pop($parts);
That way, you have no problems with anchors (#content) or query parameters (?id=123)
I'd use preg_match:
if (preg_match('#.*/(.*)#', $val, $match)) {
$result = $match[1];
}
You could also use something as simple as strrpos and substr:
$result = substr($val, strrpos($val, '/') + 1);
Under the following conditions:
The product (abc12345) is always at the end of the url
No querystrings/params
Try the following regular expression:
preg_match('/\/([\w]+)\/?$/', $val, $arr);
$product = $arr[1];
I have the following URI:
/belt/belts/fk/product/40P35871
And I want to retrieve the last content after the last /.
In this case is 40P35871.
How can I do this?
How about explode?
$elements = explode('/', $input);
$productId = end($elements);
Here's a different solution entirely. (and the simplest!)
Using basename
$var = "/belt/belts/fk/product/40P35871";
echo basename($var);
Output:
40P35871
You don't need regex for something simple like that. Consider using strrchr, documentation here
$lastcontent = substr(strrchr($uri, "/"), 1);
Considering this special case of $uri being a path, the best answer would be the one provided by Chtulhu.
basename will return the last part of a path, documentation here
$lastcontent = basename($uri);
Just like this
$str = '/belt/belts/fk/product/40P35871';
$arr = explode('/', $str);
$var = array_pop($arr);
var_dump($var);
or
$var = substr($str, strrpos($str,'/') + 1);
Try this
$result = preg_replace('%(/(?:[^/]+?/)+)([^/]+)\b%', '$2', $subject);
use this:
echo preg_replace('/[a-z0-9]$/i', '$1', $url);
this will give you the last position
note: but on this url only, query strings make this useless and use need to parse the url for the same first for this to work
Don't use regex. In this case you can act as the follow
myUrl = $_SERVER[REQUEST_URL];
$number = substr(strrpos(myUri,'/')+1);
You don't need regex.
Find the last content and get it using substr():
$lastcontent = substr(strrchr($uri, "/"), 1);
I have a string that looks a little like this, world:region:bash
It divides folder names, so i can create a path for FTP functions.
However, i need at some points to be able to remove the last part of the string, so, for example
I have this world:region:bash
I need to get this world:region
The script wont be able to know what the folder names are, so some how it needs to be able to remove the string after the last colon.
$res=substr($input,0,strrpos($input,':'));
I should probably highlight that strrpos not strpos finds last occurrence of a substring in given string
$tokens = explode(':', $string); // split string on :
array_pop($tokens); // get rid of last element
$newString = implode(':', $tokens); // wrap back
You may want to try something like this:
<?php
$variable = "world:region:bash";
$colpos = strrpos($variable, ":");
$result = substr($variable, 0, $colpos);
echo $result;
?>
Or... if you create a function using this information, you get this:
<?php
function StrRemoveLastPart($string, $delimiter)
{
$lastdelpos = strrpos($string, $delimiter);
$result = substr($string, 0, $lastdelpos);
return $result;
}
$variable = "world:region:bash";
$result = StrRemoveLastPart($variable, ":");
?>
Explode the string, and remove the last element.
If you need the string again, use implode.
$items = array_pop(explode(':', $the_path));
$shotpath = implode(':', $items);
Use regular expression /:[^:]+$/, preg_replace
$s = "world:region:bash";
$p = "/:[^:]+$/";
$r = '';
echo preg_replace($p, $r, $s);
demo
Notice how $ which means string termination, is made use of.
<?php
$string = 'world:region:bash';
$string = implode(':', explode(':', $string, -1));
Here is my string:
**tag:my.domain.com,2011-07-13:/895645783/posts/NHg5XdqFb5b/**
I want to take the last section /NHg5XdqFb5b/ and remove the slashes.
Also are there any tools available to attemtp to work this out?
You can do this by:
<?php
$id = explode("/", "**tag:my.domain.com,2011-07-13:/895645783/posts/NHg5XdqFb5b/**");
$myID = $id[count($id)-2];
?>
Or if you want to use regex: (make sure all ids are 11 in length)
preg_match("/[a-zA-Z0-9]{11}/i", "**tag:my.domain.com,2011-07-13:/895645783/posts/NHg5XdqFb5b/**", $matches);
echo($matches[0]);
You could do a preg_replace http://ch.php.net/preg_replace
$var = preg_replace('~.*/([^/]+)/\*\*~','$1',$var);
You can use explode, which will split the string and return an array
$arr = explode("/", your_string_here); //split string by "/"
$id = $arr[count($arr) - 2]; //in your case, get the second-last part
Alternatively,
$arr = preg_match("\/posts\/(.*?)\/", your_string_here); //matches /posts/NHg5XdqFb5b/
//$arr[0] = whole match
//$arr[1] = 1st capture group (part between brackets) in your regex, i.e. required id
$id = $arr[1];
Cheers,