I have a string with the format:
$string = 'First\Second\Third';
'First' and 'Second' are always the same but Third' is just and an example (can be anything but in with special character '\' between them).
I want to create a string like that:
$string = 'Second_Third';
I tried to use function preg_replace and this is my code:
preg_replace(Array('/^First\\\/', '/Second\\\[^\s]+/'), Array('', '/Second\_[^\s]+/'), $string);
I have no idea how to do this.
10x
You can use:
$str = preg_replace('/.*?\\\\(Second)\\\\(.+)$/', '$1_$2');
RegEx Demo
If you're just looking for \ then you don't need a regular expression. You can do a regular character match. Using list and explode you can do it like so:
list($first, $second) = explode("\\", "First\\Second\\Third");
echo $first . "_" . $second;
Note that you need to escape \ because it's the escape character :>
Related
I'm trying to get the parent "elements" name as string from a PHP namespace in a string, the ideia is to do:
Input: \Base\Util\Review; Desired Output: \Base\Util;
My main problem here is how can I deal with the backslash escaping in the regex expression, so far I can make it work with the normal slash:
$ns = "/Base/Util/Review";
print preg_replace("#\/[^/]*$#", '', $ns);
// Outputs => /Base/Util
Thank you.
This should work:
$s = '\\Base\\Util\\Review';
$r = preg_replace('~\\\\[^\\\\]*$~', '', $s);
//=> \Base\Util
You can do it without preg_replace
$ns = "\Base\Util\Review";
print implode('\\', array_slice(explode('\\',$ns),0,-1));
Another option:
$ns = "\Base\Util\Review";
print substr($ns,0,strrpos($ns,'\\'));
Try this
print preg_replace("#\\\[^\\\]*$#", '', $ns)
You will have to write four backslashes for each literal backslash:
$regex = '#\\\\#'; // regex matching one backslash
You need to escape each \ once to escape its special meaning in the regex, and again escape each \ once to escape its meaning in the PHP string literal.
I have the following strings:
Johnny arrived at BOB
Peter is at SUSAN
I want a function where I can do this:
$string = stripWithWildCard("Johnny arrived at BOB", "*at ")
$string must equal BOB. Also if I do this:
$string = stripWithWildCard("Peter is at SUSAN", "*at ");
$string must be equal to SUSAN.
What is the shortest way to do this?
A regular expression. You substitute .* for * and replace with the empty string:
echo preg_replace('/.*at /', '', 'Johnny arrived at BOB');
Keep in mind that if the string "*at " is not hardcoded then you also need to quote any characters which have special meaning in regular expressions. So you would have:
$find = '*at ';
$find = preg_quote($find, '/'); // "/" is the delimiter used below
$find = str_replace('\*', '.*'); // preg_quote escaped that, unescape and convert
echo preg_replace('/'.$find.'/', '', $input);
How can I insert symbol '+' after each char of a string?
Like changing from mystring to m+y+s+t+r+i+n+g+.
You can also use this:
print implode("+", str_split($string));
To add one extra + after, just concatenate . "+".
Note: this approach is fast enough for not very long strings. Another way is to use regular expressions as illustrated in #zerkms answer.
$str = 'string';
echo preg_replace('~.~', '\\0+', $str);
You can use preg_replace:
$text = 'mystring';
// To match only characters (no numbers):
$replaced = preg_replace("/([a-z])/i", "$1+", $text);
// To match both
$replaced = preg_replace("/([a-z0-9])/i", "$1+", $text);
I would like to pass characters like A,B,C,D like this to a preg_match function in PHP.
Eg >
if (preg_match ("/^SEQRES.*\s$char\s.*/", $amino)) ;
I would like to pass A,B,C,D to $char and loop through the match.
please give me a solution.
Just use string concatenation:
$re = "/^SEQRES.*\s" . $char . "\s.*/";
if (preg_match($re, $amino))
....
There is no need for a loop. Simply replace $char with "[A-D]";. Since you have bounded it by spaces, it will match one single character in the class [A-D] between spaces .
if (preg_match ("/^SEQRES.*\s[A-D]\s.*/", $amino)) ;
You may also wish to use preg_match_all().
Use preg_quote if you are getting the character from user input:
$re = "/^SEQRES.*\s" . preg_quote($char) . "\s.*/";
I'm removing certain characters from the string by substituting them:
% -> %%
: -> %c
/ -> %s
The string "%c" is properly escaped into %%c. However when I try to reverse it back with str_replace( array('%%','%c','%s'), array('%',':','/'), $s) it converts it into ":". That's proper behaviour of str_replace as per documentation, that's why I'm looking into solution with Regular Expressions.
Please suggest, what should I use to properly decode escaped string. Thank you.
You need to do the replacement for all escape sequences at once and not successively:
preg_replace_callback('/%([%cs])/', function($match) {
$trans = array('%' => '%', 'c' => ':', 's' => '/');
return $trans[$match[1]];
}, $str)
You could use a preg_replace pipeline (with a temporary marker):
<?php
$escaped = "Hello %% World%c You'll find your reservation under %s";
echo preg_replace("/%TMP/", "%",
preg_replace("/%s/", "/",
preg_replace("/%c/", ":",
preg_replace("/%%/", "%TMP", $escaped)));
echo "\n";
# Output should be
# Hello % World: You'll find your reservation under /
?>
Judging from your comment (that you want to go from "%%c" to "%c", and not from "%%c" straight to ":"), you can use Gumbo's method with a little modification, I think:
$unescaped = preg_replace_callback('/%(%[%cs])/', function($match) {
return $match[1];
}, $escaped);