I am having a problem a problem with retrieving parenthesis value from url. I am developing a PHP project using CodeIgniter Framework.
I have a mvc url format like this - http://localhost/controller/action/id/name
Then I passed the value like this - http://localhost/controller/action/2/test%20(test)
And like this as well - Then I passed the value like this:
http://localhost/controller/action/2/test+(test)
So my action function is like this with parameters-
function action($id,$name)
{
$name = urldecode($name)
//I find the index of start "t"
echo strpos($name,'t'); //this is working find
echo strpos($name,'('); // this is outputing nothing
echo $name; // this is outputing test (test)
}
What is the problem ? Why cannot I find index of "(". Actually I and retrieving record from database using that value. But parenthesis is not working well with php. I showing output correctly. But cannot find index of it.
Finally I found the solution. That is the issue with Codeigniter Framework. I replaced all "(" and ")" with "[" and "]" before urlencoding. Then I replaced all "[" and "]" with "(" and ")" after urldecoding. You will get the answer after you read this article. http://qubitlogs.com/PHP/2013/01/24/parentheses-in-urls-not-working-codeigniter-datamapper-orm/ . Thank everyone for helping me.
Try like this with preg_match:
<?php
preg_match('/\((.*?)\)/',urldecode($name),$a);
echo $a[1];
Try this
<?php
$url = 'localhost/melomap_music/singers/details/760/test%20(test)';
$string = urldecode($url);
$pattern1 = '/[)]/';
$pattern2 = '/[(]/';
preg_match($pattern1, $string, $matches, PREG_OFFSET_CAPTURE);
preg_match($pattern2, $string, $matches2, PREG_OFFSET_CAPTURE);
$start = $matches2[0][1]; //49
$end = $matches[0][1]; //54
?>
Related
I have a question which relates to PHP. Is there any way to just grab the last string using a delimiter?
For example, to put you in perspective, I want to grab the lastest content from the / from every string.
/folder1/blabla/important
/folder2/blabla/blabla/bla/important2
How could I using PHP cut the whole string and get only the latest value. The output would be something like:
/important
/important2
I have tried regex but I am not very good at it,
MY CODE:
$var = 'goldfe/sfksfksk/admadmadmam/akdmasdkasm/red';
echo $var . '<br>';
$varPretty = explode('/', $var);
echo $varPretty[-1];
Here the $varPretty[-1] should return red.
ANOTHER SOLUTION IS:
$var = 'goldfe/sfksfksk/admadmadmam/akdmasdkasm/red';
echo $var . '<br>';
$varPretty = explode('/', $var);
echo end($varPretty);
Please try this method will work for you:
$txt = "/folder1/blabla/important";
echo substr($txt, strrpos($txt, '/') + 1);
Output should be like:
important
i have a path like this:
/dude/stuff/lol/bruh.jpg
i want to echo it like this:
/stuff/lol/bruh.jpg
how to do this? and if you could please explain it. i found one good answer
Removing part of path in php but i cannot work my way around with explode and implode.
please give me the link to the duplicate answer if my question is a duplicate
You could use strpos with substr:
$string = '/dude/stuff/lol/bruh.jpg';
$string = substr($string, strpos($string, '/', 1));
The strpos to look for the position of the / after the first one, then use substr to get the string starting from that position till the end.
$path = '/dude/stuff/lol/bruh.jpg';
$path = explode('/', $path);
unset($path[1]);
$path = implode('/', $path);
Will separate the string into an array, then unset the first item (technically the second in the array, since the first element will be empty due to the string starting with /). Finally the path is imploded, and you get:
/stuff/lol/bruh.jpg
Try this :
$string = "/dude/stuff/lol/bruh.jpg";
$firstslash = strpos($string,"/",1);
$secondstring = substr($string, $firstslash+1);
echo "String : " . $string;
echo " <br> Result : " . $secondstring;
You can use a regular expression to extract resource name you need.
preg_match('/(?P<name>[^\/]+)$/', $path, $match);
Then you have to use $match as array, and you can see the name inside of it.
$match['name];
I want to change this url from:
https://lh3.googleusercontent.com/-5EoWQXUJMiA/VZ86O7eskeI/AAAAAAADHGs/ej6F-va__Ig/s1600/i2Fun.com-helpful-dogs-015.gif
to this:
http://3.bp.blogspot.com/-5EoWQXUJMiA/VZ86O7eskeI/AAAAAAADHGs/ej6F-va__Ig/s1600/i2Fun.com-helpful-dogs-015.gif
This is my code, but it's not working as expected:
$link = preg_replace('#^https?://.*?/(.+?/)(s\d+/)?([\w_-]+\.[\w]{3,})?$#i','http://3.bp.blogspot.com/$1s0/$3',$url);
It's a simple string replace.
Search for "https://lh3.googleusercontent.com/". Replace with "http://3.bp.blogspot.com/".
str_replace() will do this. Am I missing something?
$s = "https://lh3.googleusercontent.com/-5EoWQXUJMiA/VZ86O7eskeI/AAAAAAADHGs/ej6F-va__Ig/s1600/i2Fun.com-helpful-dogs-015.gif";
$path = parse_url($s);
echo 'http://3.bp.blogspot.com' . $path['path'];
UPDATE You can don't receive all the parts of an url, but take only the needed part
echo 'http://3.bp.blogspot.com' . parse_url($s, PHP_URL_PATH);
Im trying to search for two HTML tags, and also grab everything in between. When I try each of the tags separately it finds them fine, so I know that they are spelled right and exist. The problem I think is with the pattern, can someone please help me with it.
Everything I research online seems to go right over my head. So if you could please explain how your pattern works, that would be awesome!
See code below, and if you have any questions feel free to ask.
Thanks for your time:)
<?php
$date= date(Y)."/".date(n)."/".date(j);
$address= "http:www.example.com/".$date;
$text_page = file_get_contents("$address");
$searchfor1 = '<li id="menuSynchronizeSwitch">';
$searchfor2 = '<li id="footerPrevWeek';
header('Content-Type: text/plain');
$pattern1 = preg_quote($searchfor1, '/');
$pattern2 = preg_quote($searchfor2, '/');
$pattern = "/^.*$pattern1.*\r*$pattern2.*\$/m";
if(preg_match_all($pattern, $text_page, $matches)){
echo "Found matches:\n";
echo implode("\n", $matches[0]);
}
else{
echo "No matches found";
}
?>
Try using $pattern = '/\<li id\="menuSynchronizeSwitch"\>.*\<li id\="footerPrevWeek/si';. Note the /s, which allows the dot to match newlines, which I suspect is what is causing this to fail.
You'll need to group each pattern with parenthesis
like this:
$pattern = "/(some pattern)/";
and you can get each group by going through the $matches array
i have this URI.
http://localhost/index.php?properties&status=av&page=1
i am fetching basename of the URI using following code.
$basename = basename($_SERVER['REQUEST_URI']);
the above code gives me following string.
index.php?properties&status=av&page=1
i would want to remove the last variable from the string i.e &page=1. please note the value for page will not always be 1. keeping this in mind i would want to trim the variable this way.
Trim from the last position of the string till the first delimiter i.e &
Update :
I would like to remove &page=1 from the string, no matter in which position it is on.
how do i do this?
Instead of hacking around with regular expression you should parse the string as an url (what it is)
$string = 'index.php?properties&status=av&page=1';
$parts = parse_url($string);
$queryParams = array();
parse_str($parts['query'], $queryParams);
Now just remove the parameter
unset($queryParams['page']);
and rebuild the url
$queryString = http_build_query($queryParams);
$url = $parts['path'] . '?' . $queryString;
There are many roads that lead to Rome. I'd do it with a RegEx:
$myString = 'index.php?properties&status=av&page=1';
$myNewString = preg_replace("/\&[a-z0-9]+=[0-9]+$/i","",$myString);
if you only want the &page=1-type parameters, the last line would be
$myNewString = preg_replace("/\&page=[0-9]+/i","",$myString);
if you also want to get rid of the possibility that page is the only or first parameter:
$myNewString = preg_replace("/[\&]*page=[0-9]+/i","",$myString);
Thank you guys but i think i have found the better solution, #KingCrunch had suggested a solution i extended and converted it into function. the below function can possibly remove or unset any URI variable without any regex hacks being used. i am posting it as it might help someone.
function unset_uri_var($variable, $uri) {
$parseUri = parse_url($uri);
$arrayUri = array();
parse_str($parseUri['query'], $arrayUri);
unset($arrayUri[$variable]);
$newUri = http_build_query($arrayUri);
$newUri = $parseUri['path'].'?'.$newUri;
return $newUri;
}
now consider the following uri
index.php?properties&status=av&page=1
//To remove properties variable
$url = unset_uri_var('properties', basename($_SERVER['REQUEST_URI']));
//Outputs index.php?page=1&status=av
//To remove page variable
$url = unset_uri_var('page', basename($_SERVER['REQUEST_URI']));
//Outputs index.php?properties=&status=av
hope this helps someone. and thank you #KingKrunch for your solution :)
$pos = strrpos($_SERVER['REQUEST_URI'], '&');
$url = substr($_SERVER['REQUEST_URI'], 0, $pos - 1);
Documentation for strrpos.
Regex that works on every possible situation: /(&|(?<=\?))page=.*?(?=&|$)/. Here's example code:
$regex = '/(&|(?<=\?))page=.*?(?=&|$)/';
$urls = array(
'index.php?properties&status=av&page=1',
'index.php?properties&page=1&status=av',
'index.php?page=1',
);
foreach($urls as $url) {
echo preg_replace($regex, '', $url), "\n";
}
Output:
index.php?properties&status=av
index.php?properties&status=av
index.php?
Regex explanation:
(&|(?<=\?)) -- either match a & or a ?, but if it's a ?, don't put it in the match and just ignore it (you don't want urls like index.php&status=av)
page=.*? -- matches page=[...]
(?=&|$) -- look for a & or the end of the string ($), but don't include them for the replacement (this group helps the previous one find out exactly where to stop matching)
You could use a RegEx (as Chris suggests) but it's not the most efficient solution (lots of overhead using that engine... it's easy to do with some string parsing:
<?php
//$url="http://localhost/index.php?properties&status=av&page=1";
$base=basename($_SERVER['REQUEST_URI']);
echo "Basename yields: $base<br />";
//Find the last ampersand
$lastAmp=strrpos($base,"&");
//Filter, catch no ampersands found
$removeLast=($lastAmp===false?$base:substr($base,0,$lastAmp));
echo "Without Last Parameter: $removeLast<br />";
?>
The trick is, can you guarantee that $page will be stuck on the end? If it is - great, if it isn't... what you asked for may not always solve the problem.