Extract String From String in PHP - php

I have string variable like ABC/DF/G I want extract DF from it means text between two /.
I have tried like below
$string= "ABC/DF/G";
$code = substr($string ,strpos($string,'/'));
echo $code;
but I am getting result like below
/DF/G
Let me know if someone can help me for do it.
Thanks!

You can use explode function to split a string into an array.
$string= "ABC/DF/G";
$code = explode('/', $string);
echo $code[1];

Related

PHP regex: How to extract url and filter "\" from specific string

I'm a newbie with programming; now, I have some problems with PHP programing.
I want to extract the URL from the following stringļ¼š
"success":true,"load":"http:\/\/8.88.88.8\/list\/si3diwoe\/123","Live":true
The desired string is
http://8.88.88.8/list/si3diwoe/123
Can anyone tell me how this work in code?
Thank you very much!
$string = '"success":true,"load":"http:\/\/8.88.88.8\/list\/si3diwoe\/123","Live":true';
// The JSON way
$array = json_decode('{' . $string . '}'); // Wrap in {} to make object
$url = $array->load;
echo "Using JSON : $url", PHP_EOL;
// The RegEx way
preg_match('/load":"(.*?)"/', $string, $matches);
$url = stripslashes($matches[1]);
echo "Using RegEx: $url", PHP_EOL;
Output:
Using JSON : http://8.88.88.8/list/si3diwoe/123
Using RegEx: http://8.88.88.8/list/si3diwoe/123
If it's a json string, you can use json_decode to assign it to a variable, say $arr, then stripslashes($arr['load']);
if it's not, you can use explode("," , $string), and assign it to a variable, say $arr again. Run explode again (explode(":", $arr[1])) and assigned it to another variable, say $ar, then do stripslashes on $ar['1'];

How to remove part of the string from one character to another in php

I have a URL string like
http://mydomain.com/status/statusPages/state/stack:3/my_link_id:1#state-9
I want to remove my_link_id:1 from this string. I know I can use any string replace function like
$goodUrl = str_replace('my_link_id:1', '', $badUrl);
But the problem is, the integer part is dynamic. I mean in my_link_id:1 the 1 is dynamic. It is the ID of the link and it can be from 0 to any number. So I want to remove my_link_id:along with any dynamic number from the string.
What I think I should remove part of the string from last / to #. But how can I do that?
you can use regular expressions:
$goodUrl = preg_replace('/(my_link_id:[0-9]+)/ig', '', $badUrl);
You can use preg_replace() php function
http://in1.php.net/preg_replace
CODE:
<?php $string = "http://mydomain.com/status/statusPages/state/stack:3/my_link_id:1#state-9";
echo $result = preg_replace('/(my_link_id:[0-9]+)/si', '', $string);
?>
Output :
http://mydomain.com/status/statusPages/state/stack:3/#state-9
May following help you
$strUrl = "http://mydomain.com/status/statusPages/state/stack:3/my_link_id:1#state-9";
$finalUrl = preg_replace('/(my_link_id:[0-9]+)/i', '', $strUrl);
echo $finalUrl;
and If you want remove also last / and # you follow this code
$strUrl = "http://mydomain.com/status/statusPages/state/stack:3/my_link_id:1#state-9";
$finalUrl = preg_replace('/(\/my_link_id:[0-9]+#)/i', '', $strUrl);
echo $finalUrl;

preg_replace a part of a string with variable

I have a string that looks a little something like this:
$string = 'var1=foo&var2=bar&var3=blahblahblah&etc=etc';
And I would like to make another string from that, and replace it with a value, so for example it will look like this
$newstring = 'var1=foo&var2=bar&var3=$myVariable&etc=etc';
so var3 in the new string will be the value of $myVariable.
How can this be achieved?
No need for regex; built in URL functions should work more reliably.
// Parse the url first
$url = parse_url($string);
// Parse your query string into an array
parse_str($url['query'], $qs);
// Replace the var3 key with your variable
$qs['var3'] = $myVariable;
// Convert the array back into a query string
$url['query'] = http_build_query($qs);
// Convert the $url array back into a URL
$newstring = http_build_url($url);
Check out http_build_query and parse_str. This will append a var3 variable even if there isn't one, it will URL encode $myVariable, and its more readable than using preg_replace.
You can use the method preg_replace for that. Here comes an example
<?php
$string = 'var1=foo&var2=bar&var3=blahblahblah&etc=etc';
$var = 'foo';
echo preg_replace('/var3=(.*)&/', 'var3=' . $var . '&', $string);

PHP get query string from a string

I have a string with a URL
$string = "http://www.yahoo.com/foo.php?bar=201";
all i want is the query string without everything else.
obviously cant user query_string, help appreciated.
$parts = parse_url($string);
echo $parts['query'];
http://php.net/manual/en/function.parse-url.php

Increment integer at end of string

I have a string, "Chicago-Illinos1" and I want to add one to the end of it, so it would be "Chicago-Illinos2".
Note: it could also be Chicago-Illinos10 and I want it to go to Chicago-Illinos11 so I can't do substr.
Any suggested solutions?
Complex solutions for a really simple problem...
$str = 'Chicago-Illinos1';
echo $str++; //Chicago-Illinos2
If the string ends with a number, it will increment the number (eg: 'abc123'++ = 'abc124').
If the string ends with a letter, the letter will be incremeted (eg: '123abc'++ = '123abd')
Try this
preg_match("/(.*?)(\d+)$/","Chicago-Illinos1",$matches);
$newstring = $matches[1].($matches[2]+1);
(can't try it now but it should work)
$string = 'Chicago-Illinois1';
preg_match('/^([^\d]+)([\d]*?)$/', $string, $match);
$string = $match[1];
$number = $match[2] + 1;
$string .= $number;
Tested, works.
explode could do the job aswell
<?php
$str="Chicago-Illinos1"; //our original string
$temp=explode("Chicago-Illinos",$str); //making an array of it
$str="Chicago-Illinos".($temp[1]+1); //the text and the number+1
?>
I would use a regular expression to get the number at the end of a string (for Java it would be [0-9]+$), increase it (int number = Integer.parse(yourNumberAsString) + 1), and concatenate with Chicago-Illinos (the rest not matched by the regular expression used for finding the number).
You can use preg_match to accomplish this:
$name = 'Chicago-Illinos10';
preg_match('/(.*?)(\d+)$/', $name, $match);
$base = $match[1];
$num = $match[2]+1;
print $base.$num;
The following will output:
Chicago-Illinos11
However, if it's possible, I'd suggest placing another delimiting character between the text and number. For example, if you placed a pipe, you could simply do an explode and grab the second part of the array. It would be much simpler.
$name = 'Chicago-Illinos|1';
$parts = explode('|', $name);
print $parts[0].($parts[1]+1);
If string length is a concern (thus the misspelling of Illinois), you could switch to the state abbreviations. (i.e. Chicago-IL|1)
$str = 'Chicago-Illinos1';
echo ++$str;
http://php.net/manual/en/language.operators.increment.php

Categories