PHP preg_replace url get parameters in string - php

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..

Related

I need to replace all instances of [IMG] in a string using PHP

I've tried:
$input = str_replace('[IMG]',"",$input,$rplc);
with no success.
I've also tried escaping and double-escaping [IMG] by doing '[IMG]' and '\[IMG\]', neither worked. It doesn't throw any errors, but it doesn't actually replace anything either.
How can I get this to work?
Have you tried something like:
$input = preg_replace("/(\\[IMG\\])/", $replacement, $input);
Just try this example, to implement within your code..
$search = "[IMG]";
$replace = "";
$string = "Hello [IMG] Approxx? How[IMG] are [IMG]You?";
$input = str_replace($search, $replace, $string);
echo $input;
Try this
preg_replace("#\[img\]#","",$input);
And the same result can also be achived by using
str_replace()
str_replace("[img]",'',$input);

php preg_match_all preg_replace array issue

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.

Ignore specific character: -

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);

How to filter a string

I have this string:
"small:
http://img.exent.com/free/frg/products/666550/player_boxshot.jpg
boxshot:
http://img.exent.com/free/frg/products/666550/boxshot.jpg"
I want to get only the first image link (after the word "small") and ignore all the rest (the word "boxshot" and the link after it).
How can I make it?
use it and like my ans. string is your above whole string
substr(string,0,stripos(string,"boxshot")-1);
$string = "small:
http://img.exent.com/free/frg/products/666550/player_boxshot.jpg
boxshot:
http://img.exent.com/free/frg/products/666550/boxshot.jpg";
preg_match_all('~http(.*?)jpg~i',trim($string),$matches);
var_dump($matches[0][0]);
The easy way:
<?php
$str='small: http://img.exent.com/free/frg/products/666550/player_boxshot.jpg boxshot: http://img.exent.com/free/frg/products/666550/boxshot.jpg';
$parts=explode('boxshot:',$str);
$small=$parts[0];
echo($small);
?>
Try this:
$string = "small:
http://img.exent.com/free/frg/products/666550/player_boxshot.jpg
boxshot:
http://img.exent.com/free/frg/products/666550/boxshot.jpg";
// replace all 2 or more consecutive spaces with 1 space
$string = preg_replace( '/\s\s+/i', ' ', $string);
$array = explode( ' ', $string);
echo $array[1];
Hope this helps.

Parsing a Source With REGEX

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]+)/';

Categories