This question is more of a "what is the best/easiest way to do"-type-of-question. I would like to grab just the users id from a string such as
User name
I would like to parse the string and get just the "123456" part of it.
I was thinking I could explode the string but then I would get id=123456&blahblahblah and I suppose I would have to somehow dynamically remove the trash from the end. I think this may be possible with regex but I'm fairly new to PHP and so regex is a little above me.
The function parse_str() will help here
$str = "profile.php?rdc332738&id=123456&refid=22";
parse_str($str);
echo $id; //123456
echo $refid; //22
Just grab any character from id= up to & or " (the latter accounts for the case where id is put last on the query string)
$str = 'a href="/profile.php?rdc332738&id=123456&refid=22">User name</a>';
preg_match('/id=([^&"]+)/', $str, $match);
$id = $match[1];
Regex:
.*id[=]([0-9]*).*$
if you explode the string:
$string="User name"
$string_components=explode('&','$string');
The user id part (id=123456) will be:
$user_id_part=$string_components[1];
then you could do a string replace:
$user_id=str_replace('id=','$user_id_part');
And you have the user id
Related
I have a url from where i am fetching value using GET method and i want to replace that value with the 4 digit random number in the string like
This is my main URL:
http://localhost/ab/index.php?id=3345
These are the strings in my table (fetching from database):
http://anyurl/index.php?id=4876&abc=any
http://anyurl/index.php?id=8726&abc=any
http://anyurl/index.php?id=9026&abc=any
So whenever i open the main url the id's of the table should be replaced according to the main url
you can get the id parameter using global GET variable
$id = $_GET["id"]
then you can change the urls in the table according to it
$url = "http://anyurl/index.php?id=".$id."&abc=any"
Hope this will help you
If you want to replace the id with preg_replace in string then you can do like below:
<?php
$string = 'http://anyurl/index.php?id=4876&abc=any';
$new_string = preg_replace('/[0-9]+/', $_GET["id"], $string);
echo $new_string;
// Will display http://anyurl/index.php?id=3345&abc=any
?>
I know that it was asked years ago, but, probably, someone will find my solution helpful
So, I also offer to use preg_replace(), as Amit Gupta, but improve it for cases when you could have other numbers before ID value:
$url = 'http://anyurl/index.php?foo=0713&id=4876&abc=any';
$new_id = $_GET['id'];
// regex: catch 1 or more digits after 'id='
$new_url = preg_replace( '/id=(\d+)/', $new_id, $url );
If $_GET['id'] is 4920, for example, $new_url will be equal to http://anyurl/index.php?foo=0713&id=4920&abc=any
sorry if my question was stupid, please someone help me to fix this issue.
i have string like
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/9/";
this $str_value is dynamic , it will change each page. now i need to replace 9 in this string as 10. add integer 1 and replace
for example if the $str_value = "http://99.99.99.99/var/test/src/158-of-box.html/251/"
then output should be
http://99.99.99.99/var/test/src/158-of-box.html/252/
i tried to replace using preg_match but i m getting wrong please somesone help me
$str = preg_replace('/[\/\d+\/]/', '10',$str_value );
$str = preg_replace('/[\/\d+\/]/', '[\/\d+\/]+1',$str_value );
Thank's for the answer, #Calimero! You've been faster than me, but I would like to post my answer, too ;-)
Another possibilty is to fetch the integer by using a group. So you don't need to trim $matches[0] to remove the slashes.
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/9/";
$str = preg_replace_callback('/\/([\d+])\//', function($matches) {
return '/'.($matches[1]+1).'/';
}, $str_value);
echo $str;
You need to use a callback to increment the value, it cannot be done directly in the regular expression itself, like so :
$lnk= "http://99.99.99.99/var/test/src/158-of-box.html/9/";
$lnk= preg_replace_callback("#/\\d+/#",function($matches){return "/".(trim($matches[0],"/")+1)."/";},$lnk); // http://99.99.99.99/var/test/src/158-of-box.html/10/
Basically, the regexp will capture a pure integer number enclosed by slashes, pass it along to the callback function which will purge the integer value, increment it, then return it for replacement with padded slashes on each side.
I'd suggest also another approach based on explode and implode instead of doing any regexp stuff. In my opinion this is more readable.
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/11/";
// explode the initial value by '/'
$explodedArray = explode('/', $str_value);
// get the position of the page number
$targetIndex = count($explodedArray) - 2;
// increment the value
$explodedArray[$targetIndex]++;
// implode back the original string
$new_str_value = implode('/', $explodedArray);
I'm extracting twitter user's profile image through JSON. For this my code is:
$x->profile_image_url
that returns the url of the profile image. The format of the url may be "..xyz_normal.jpg" or "..xyz_normal.png" or "..xyz_normal.jpeg" or "..xyz_normal.gif" etc.
Now I want to delete the "_normal" part from every url that I receive. How can I achieve this in php? I'm tired of trying it. Please help.
Php str_replace.
str_replace('_normal', '', $var)
What this does is to replace '_normal' with '' (nothing) in the variable $var.
Or take a look at preg_replace if you need the power of regular expressions.
The str_ireplace() function does the same job but ignoring the case
like the following
<?php
echo str_ireplace("World","Peter","Hello world!");
?>
output : Hello Peter!
for more example you can see
The str_replace() function replaces some characters with some other characters in a string.
try something like this:
$x->str_replace("_normal","",$x)
$s = 'Posted On jan 3rd By Some Dude';
echo strstr($s, 'By', true);
This is to remove particular string from a string.
The result will be like this
'Posted On jan 3rd'
Multi replace
$a = array('one','two','three');
$var = "one_1 two_2 three_3";
str_replace($a, '',$var);
string erase(subscript, count)
{
string place="New York";
place erase(0,2)
}
I know there are lots of tutorials and question on replacing something in a string.
But I can't find a single one on what I want to do!
Lets say I have a string like this
$string="Hi! [num:0]";
And an example array like this
$array=array();
$array[0]=array('name'=>"na");
$array[1]=array('name'=>"nam");
Now what I want is that PHP should first search for the pattern like [num:x] where x is a valid key from the array.
And then replace it with the matching key of the array. For example, the string given above should become: Hi! na
I was thinking of doing this way:
Search for the pattern.
If found, call a function which checks if the number is valid or not.
If valid, returns the name from the array of that key like 0 or 1 etc.
PHP replaces the value returned from the function in the string in place of the pattern.
But I can't find a way to execute the idea. How do I match that pattern and call the function for every match?
This is just the way that I am thinking to do. Any other method will also work.
If you have any doubts about my question, please ask in comments.
Try this
$string="Hi! [num:0]";
$array=array();
$array[0]=array('name'=>"na");
$array[1]=array('name'=>"nam");
echo preg_replace('#(\!)?\s+\[num:(\d+)\]#ie','isset($array[\2]) ? "\1 ".$array[\2]["name"] : " "',$string);
If you don't want the overhead of Regex, and your string format remains same; you could use:
<?php
$string="Hi! [num:0]";
echo_name($string); // Hi John
echo "<br />";
$string="Hello! [num:10]";
echo_name($string); // No names, only Hello
// Will echo Hi + Name
function echo_name($string){
$array=array();
$array[0]=array('name'=>"John");
$array[1]=array('name'=>"Doe");
$string = explode(" ", $string);
$string[1] = str_replace("[num:", "", $string[1]);
$string[1] = str_replace("]", "", $string[1]);
if(array_key_exists($string[1], $array)){
echo $string[0]." ".$array[$string[1]]["name"];
} else {
echo $string[0]." ";
}
}// function echo_sal ENDs
?>
Live: http://codepad.viper-7.com/qy2uwW
Assumptions:
$string always will have only one space, exactly before [num:X].
[num:X] is always in the same format.
Of course you could skip the str_replace lines if you could make your input to simple
Hi! 0 or Hello! 10
I'm making my own forums and I don't want any BB code on it, but instead my own,
so i've gotten [b][u][img] working etc.
But i'm having problems with [quote=1][/quote] where the number is the user id...
E.G lets say I quote someone
So once I submit my post: (The variable $post would be:)
'[quote=1] Quoted post :P[/quote]'
How would I then get the number out the string? (But not the wrong number -not a number in the quoted post)
(So I could then use str_replace() to replace with a table which makes it looked quoted)
?? :)
\[quote=([0-9]*)\] and grab the captured string $1
$pattern = "{\[quote=([0-9]*)\](.*)\[\/quote\]}";
$subject = $post;
preg_match($pattern, $subject, $matches);
//$matches[0] contains the whole string
//$matches[1] contains the id
It is very common to use regular expressions in order to implement BB-Codes. Of course you could use something like str_replace, but you will probably get some problems later.
Use the following pattern to make sure that the quote-tag gets also closed:
/\[quote=(\d+)\](.*?)\[\/quote\]/is
Now you should use preg_replace or preg_match to work with it.
For example:
echo preg_replace('/\[quote=(\d+)\](.*?)\[\/quote\]/is',
'<b>\\1 wrote:</b> \\2',
$input
);
Or:
$input = "text [quote=11]my quoted post
abc[/quote]
[quote=20]my quoted post 2[/quote]";
if(preg_match_all('/\[quote=(\d+)\](.*?)\[\/quote\]/is', $input, $matches)) {
var_dump($matches);
}
This should work for you.
$post = '[quote=1] Quoted post :P[/quote]';
if (preg_match("/\\[quote=([\d]+)\\]/",$post,$matches)) {
//echo "<pre>".print_r($matches,true)."</pre>";
$quote_user = $matches[1];
}