PHP preg_replace question? - php

hey guys,
I'm a preg_replace noob and don't understand how to solve the following case:
$youtubeurl = "((http|https)\:\/\/(www|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)\.youtube\.(com|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)([a-zA-Z0-9\-\.\/\?_=&;]*))";
$content = preg_replace($youtubeurl, embedCode($youtubeurl), $content);
I have a pattern that matches any youtube URL.
If this pattern is matched i want to call the function embedCode() and pass along the matched string.
How can i do this. right now i'm obviously passing along the regexp code which is of course wrong. I need to pass along the matched string.
thank you

Try preg_replace_callback.

$youtube_url_pattern = "#((http|https)\:\/\/(www|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)\.youtube\.(com|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)([a-zA-Z0-9\-\.\/\?_=&;]*))#";
if(preg_match($youtube_url_pattern, $content_containing_url, $content)){
embedCore($contet[index]);
}
You just have to find the index of matched url, try print_r($content); inside the if and see what is the index for the matched pattern.

You are trying to do this:
if ( preg_match( '/' . $youtubeurl . '/', $content, $match ) )
{
embedCode( $match );
}

Related

Why is this preg_match returning empty?

I’m trying to get the ID from this URL but it keeps coming back empty. Here is my code:
This is what the URLs look like on the website i'm looking on:
<b><a href="index.php?page=news&id=32662f87eb22a90d81b2362c6ff458a57643eff1"
This is my PHP
$pattern = "#<b><a href=\"index.php?page=news&id=(.*?)\"#i";
preg_match_all($pattern,$openSite,$match);
? and . are special characters. You need to add a \ before them.
$pattern = "#<b><a href=\"index\.php\?page=news&id=(.*?)\"#i";
$pattern = "#\<b\>\s*\<a\s+href\=\"index\.php\?page\=news\&amp\;id\=([^\"\&]*)#i";
You can use a simpler pattern: /id=(.*)$/
<?php
$openSite = '<b><a href="index.php?page=news&id=32662f87eb22a90d81b2362c6ff458a57643eff1"';
$pattern = "/id=(.*)$/";
preg_match_all($pattern,$openSite,$match);
print_r($match[1])
?>
OUTPUT
Array
(
[0] => 32662f87eb22a90d81b2362c6ff458a57643eff1"
)

PHP Regex Matching Image URLs

This is my Image Url PHP Code.
$GetImage = 'https://lh6.ggpht.com/hWXw7YRl9DpSMewd29xT9rvxcgnmGXeXSY9FTaPc3cbBCa-JO8yfwSynmD5C1DLglw=w124';
preg_match_all("/https://\w\w\d.\w+.com/[\w-]+=\w\d{2,3}/", $GetImage, $Result, PREG_SET_ORDER);
its working for me, but i want to extract "[\w-]" pattern results, in other words, i want to extract "hWXw7YRl9DpSMewd29xT9rvxcgnmGXeXSY9FTaPc3cbBCa-JO8yfwSynmD5C1DLglw" this string from my image Url...
Please anybody help my to solve this problem....
thanks
I feel it's overkill to try to match the entire URL using a regular expression. I suggest you parse the URL first using PHP's built-in function parse_url().
<?php
$str = 'https://lh6.ggpht.com/hWXw7YRl9DpSMewd29xT9rvxcgnmGXeXSY9FTaPc3cbBCa-JO8yfwSynmD5C1DLglw=w124';
// Parse the URL before applying a regex. Only get the path part. Use substring to remove the leading slash
$path = substr( parse_url( $str, PHP_URL_PATH ), 1 );
$pattern = '/([^=]+)/';
$matches = array();
if ( preg_match( $pattern, $path, $matches ) ) {
// Regex matched
$id = $matches[1];
// Outputs: string 'hWXw7YRl9DpSMewd29xT9rvxcgnmGXeXSY9FTaPc3cbBCa-JO8yfwSynmD5C1DLglw' (length=66)
var_dump( $id );
}
?>
Note that the snippet does not check the domain name. You can easily adjust the script to do so by not limiting the parse_url() function to only return the path, but also the other parts.
Try like this
$GetImage = 'https://lh6.ggpht.com/hWXw7YRl9DpSMewd29xT9rvxcgnmGXeXSY9FTaPc3cbBCa-JO8yfwSynmD5C1DLglw=w124';
preg_match_all('#https://.*\.com/([\w-]+=\w\d{2,3})#iU', $GetImage, $match, PREG_SET_ORDER);
print_r($match);

This script won't find Absolute Urls

in the code below, it is supposed to scan links and index them in the array [links]. but for some reason, they won't index.
I am starting to think if my regex code is wrong, how can i improve it. Also is it my file_get_contents command? Is it used correctly?
$links = Array();
$URL = 'http://www.theqlick.com'; // change it for urls to grab
// grabs the urls from URL
$file = file_get_contents($URL);
$abs_url = preg_match_all("'^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$^'", $file, $link);
if (!empty($abs_url)) {
$links[] = $abs_url;
}
In your preg_match_all you are saving into $link not $links.
preg_match_all Returns the number of full pattern matches (which might be zero), or FALSE if an error occurred (c) php.net
preg_match_all("'^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$^'", $file, $matches);
if (!empty($matches)
$links = $matches;
Your regex is wrong. You have a head anchor ^ at the end of the pattern adjacent to a tail match $. I don't think the anchors really aren't needed. Additionally, your variable you are storing matches in $link (no s). Plus your pattern delimiter appears to be the ' character. Was that intentional? It would fortunately work, but I'm guessing you didn't intend for that?
Try this:
$matchCount = preg_match_all("/(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/", $file, $matches);
if ($matchCount)
{
foreach ($matches as $match)
{
$links[] = $match[0];
}
}
Read up on PHP regular expressions.

Function to remove GET variable with php

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.

PHP - strip URL to get tag name

I need to strip a URL using PHP to add a class to a link if it matches.
The URL would look like this:
http://domain.com/tag/tagname/
How can I strip the URL so I'm only left with "tagname"?
So basically it takes out the final "/" and the start "http://domain.com/tag/"
For your URL
http://domain.com/tag/tagname/
The PHP function to get "tagname" is called basename():
echo basename('http://domain.com/tag/tagname/'); # tagname
combine some substring and some position finding after you take the last character off the string. use substr and pass in the index of the last '/' in your URL, assuming you remove the trailing '/' first.
As an alternative to the substring based answers, you could also use a regular expression, using preg_split to split the string:
<?php
$ptn = "/\//";
$str = "http://domain.com/tag/tagname/";
$result = preg_split($ptn, $str);
$tagname = $result[count($result)-2];
echo($tagname);
?>
(The reason for the -2 is because due to the ending /, the final element of the array will be a blank entry.)
And as an alternate to that, you could also use preg_match_all:
<?php
$ptn = "/[a-z]+/";
$str = "http://domain.com/tag/tagname/";
preg_match_all($ptn, $str, $matches);
$tagname = $matches[count($matches)-1];
echo($tagname);
?>
Many thanks to all, this code works for me:
$ptn = "/\//";
$str = "http://domain.com/tag/tagname/";
$result = preg_split($ptn, $str);
$tagname = $result[count($result)-2];
echo($tagname);

Categories