I want to get all Performance ID's from this page .
<?php
$content = file_get_contents("http://www124.popmundo.com/Common/Performances.asp?action=ComingPerformances&ArtistID=1962457");
$regex = "Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)";
//$regex = "/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/";
//$regex = "/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/s";
//all pattern variations tested, not working
if(preg_match_all($regex, $content, $m))
print_r($m);
else
echo "FALSE";
// this is returning FALSE
Use & instead of & in your regex.
Try this:
$regex = "/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/";
It looks like an escape problem. Not knowing php, I would guess one of these
might fix it:
$regex = 'Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)';
or
$regex = "Performances\\.asp\\?action=Arrangements&PerformanceID=([0-9]+)";
or
$regex = '/Performances\.asp\?action=Arrangements&PerformanceID=([0-9]+)/';
Related
Thanks to #s.d.a.p.e I've come a step close but I'm not quite there yet.
What I'm trying to do is replace all instances of a string in a block of text. I want to replace something like this:
user is ?user_id=34&first_name=Ralph so is ?user_id=1 also
With this:
user is /user/34/ so is /user/1/ also
Here is the preg_replace code I'm using:
$pattern = '#\?user_id=([0-9]+)#';
$replace = '/user/$1/';
echo preg_replace($pattern,$replace,$string);
With that pattern I end up with this:
user is /user/34/&first_name=Ralph so is /user/1/ also
Thanks again.
try this:
$string = "user is ?user_id=34&first_name=Ralph so is ?user_id=1 also";
$result = preg_replace('/\?(user)_id=(\d+)(.*?)(?! )/i', '/$1/$2/$3', $string );
echo $result ;
Output:
user is /user/34/&first_name=Ralph so is /user/1/ also
DEMO
I'd use this:
$string = 'user is ?user_id=34&first_name=Ralph so is ?user_id=1 also';
$pattern = '#\?user_id=([0-9]+)\S*#';
$replace = '/user/$1/';
echo preg_replace($pattern, $replace, $string);
Where \S stands for any character that is not a space.
Output:
user is /user/34/ so is /user/1/ also
print preg_replace(
'#\?user_id=([0-9]+)\&(first_name=(?:.*))#',
'/user/$1?$2',
'?user_id=34&first_name=Ralph'
);
result :
/user/34?first_name=Ralph if get it right..
I want to replace a section of a string based that starts with one string and ends with another, and I want the section between also replaced. I think this is possible using regex but I cant' seem to find any decent examples showing this.
For Example:
I have "http://www.website.com" and I want to replace from "www" to "com" with "123xyz".
So"http://www.website.com/something" becomes "http://123xyz/something.
I am assuming I have to use preg_replace(), and I think the regex should start with "^www" and end with "com$", but I cant seem to get a grasp of the syntax of regex enough to create the desired effect.
please help
With reference to your example , you can try like this
$string = 'http://www.website.com/something';
$pattern = '/www(.*)com/';
$replacement = '123xyz';
echo preg_replace($pattern, $replacement, $string);
$phrase = "http://www.website.com";
$phraseWords = array("www", "com");
$replaceTo = array("123xyz", "something");
$result = str_replace($phraseWords, $replaceTo, $phrase);
echo $result;
Thanks so much to both #CodingAnt and #PHPWeblineindia for your great answers. Using #CodingAnt's answer (and some more research I did online) I wrote this function:
function replaceBetween(&$target, $from, $to, $with){
if(strpos($target, $from)===false)return false;
$regex = "'".$from."(.*?)".$to."'si";
preg_match_all($regex, $target, $match);
$match = $match[1];
foreach($match as $m) $target = str_replace($from.$m.$to, $with, $target);
return $target;
}
It seems to work pretty well. I hope someone finds this useful.
I'm working on a bb-code replacement function when a user wants to post a smiley.
The problem is, that if someone uses a bb-code smiley that doesn't exists, it results in an empty post because the browser will not display the (non-existing) emoticon.
Here's my code so far:
// DO [:smiley:]
$convert_smiley = preg_match_all('/\[:(.*?):\]/i', $string, $matches);
if( $convert_smiley )
{
$string = preg_replace('/\[:(.*?):\]/i', "<i class='icon-smiley-$1'></i>", $string, $convert_smiley);
}
return $string;
The bb-code for a smiley usually looks like [:smile:] or like [:sad:] or like [:happy:] and so on.
The code above is working well, until someone post a bb-code that doesn't exists, so what I am asking for is a fix for non existing smileys.
Is there a possibility, in example to create an array, like array('smile', 'sad', 'happy') and only bb-code that matches one or more in this array will be converted?
So, after the fix, posting [:test:] or just [::] should not be converted and should be posted as original text while [:happy:] will be converted.
Any ideas? Thanks!
I put your possible smiley’s in non-grouping parentheses with or symbol in a regexp:
<?php
$string = 'looks like [:smile:] or like [:sad:] or like [:happy:] [:bad-smiley:]';
$string = preg_replace('/\[:((?:smile)|(?:sad)|(?:happy)):\]/i', "<i class='icon-smiley-$1'></i>", $string);
print $string;
Output:
looks like <i class='icon-smiley-smile'></i> or like <i class='icon-smiley-sad'></i> or like <i class='icon-smiley-happy'></i> [:bad-smiley:]
[:bad-smiley:] is ignored.
A simple workaround:
$string ="[:clap:]";
$convert_smiley = preg_match_all('/\[:(.*?):\]/i', $string, $matches);
$emoticons = array("smile","clap","sad"); //array of supported smileys
if(in_array($matches[1][0],$emoticons)){
//smily exists
$string = preg_replace('/\[:(.*?):\]/i', "<i class='icon-smiley-$1'></i>", $string, $convert_smiley);
}
else{
//smily doesn't exist
}
Well, the first issue is you are setting $convert_smiley to the true/false value of the preg_match_all() instead of parsing the results. Here is how I reworked your code:
// Test strings.
$string = ' [:happy:] [:frown:] [:smile:] [:foobar:]';
// Set a list of valid smileys.
$valid_smileys = array('smile', 'sad', 'happy');
// Do a `preg_match_all` against the smiley’s
preg_match_all('/\[:(.*?):\]/i', $string, $matches);
// Check if there are matches.
if (count($matches) > 0) {
// Loop through the results
foreach ($matches[1] as $smiley_value) {
// Validate them against the valid smiley list.
$pattern = $replacement = '';
if (in_array($smiley_value, $valid_smileys)) {
$pattern = sprintf('/\[:%s:\]/i', $smiley_value);
$replacement = sprintf("<i class='icon-smiley-%s'></i>", $smiley_value);
$string = preg_replace($pattern, $replacement, $string);
}
}
}
echo 'Test Output:';
echo htmlentities($string);
Just note that I chose to use sprintf() for the formatting of content & set $pattern and $replacement as variables. I also chose to use htmlentities() so the HTML DOM elements can easily be read for debugging.
I want to ignore a specific character using php. So when a user adds this character in the textbox. the php scripts filters it out first. I tried something and came up with this:
<?php
$datetogoto = $_GET['datetogoto'];
$pattern = '-';
$replace = '';
preg_replace($pattern, $replace, $datetogoto);
header('Location: ../index.php?newsdate='.$datetogoto);
?>
So what it wrong with this code?
Can you try using str_replace
$datetogoto = $_GET['datetogoto'];
$datetogoto = str_replace("-","", $datetogoto);
Ref: http://us1.php.net/str_replace
Or , if you want get date format whatever you sent in query string, then use urlencode()
header('Location: ../index.php?newsdate='.urlencode($datetogoto));
PHP regex needs delimiters, so use it like this:
$pattern = '/-/';
OR else use str_replace:
str_replace('-', $replace, $datetogoto);
function strip_cdata($string)
{
preg_match_all('/<!\[cdata\[(.*?)\]\]>/is', $string, $matches);
return str_replace($matches[0], $matches[1], $string);
}
I use the above function to extract text from cdata, but I don't know how to modify the reg expression to get a number from the url below.
http://blah.somesite.com/anytextcouldbehere/2828842087.html
Specifically, I need to get "2828842087" from the above url. It will always be numbers and be between the last "/" and ".html".
Thanks
No need to use regexp here:
$url = 'http://blah.somesite.com/anytextcouldbehere/2828842087.html';
$data = parse_url($url);
$number = basename($data['path'], '.html');
Here's the regular expression:
$url = 'http://blah.somesite.com/anytextcouldbehere/2828842087.html';
if( preg_match('#/(\d+)\.html$#', $url, $matches) ){
$number = $matches[1];
}
You can also try this one instead of regex:
$url = 'http://blah.somesite.com/anytextcouldbehere/2828842087.html';
$info = pathinfo($url);
$num = $info['filename'];
echo $num;
use this regex
/\d*/
preg_match_all('/\d*/is', $string, $matches);
you can use this:
preg_match_all('/^http\:\/\/([\w\/\.]+)\/(<?P<num>[\d]+)\.html$/',$url,$matches);
now the number is in $matches['num']. that is an array like this:
Array(0=>2828842087)
good luck