PHP: preg_replace() on NameSpace - php

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.

Related

Why doesn't PHP's preg_replace function like escaping "\"?

I use the RegExr website to test my regular expressions. On that website it can easily find the character "\" with the RegEx string /\\/g. But when I use it in php it throws back:
Warning: preg_replace(): No ending delimiter '/'
My Code
$str = "0123456789 _+-.,!##$%^&*();\/|<>";
echo preg_replace('/\\/', '', $str);
Why doesn't PHP like to escape "\"?
When using it in the regexp use \\ to use it in the replacement, use \\\\ will turn into \\ that will be interpreted as a single backslash.
Use it like this:
<?php
$str = "0123456789 _+-.,!##$%^&*();\/|<>";
echo preg_replace('/\\\\/', '', $str);
Output:
0123456789 _+-.,!##$%^&*();/|<>

Preg_replace with a part of string but in another format

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

Replace character with special characters

I have something like this :
$x = "#frac {2} {3}";
I try this code but is doesnt work:
$x = str_replace("#","\"",$x);
I want to replace # with with \ in this string. But I cant use str_replace.
any help?
You are replacing the # with a "
To use the backslash in a string you need to escape it.
How do you make a string in PHP with a backslash in it?
$x = "#frac {2} {3}";
$x = str_replace("#", "\\", $x);

Remove backslash \ from string using preg replace of php

I want to remove the backslash alone using php preg replace.
For example: I have a string looks like
$var = "This is the for \testing and i want to add the # and remove \slash alone from the given string";
How to remove the \ alone from the corresponding string using php preg_replace
why would you use preg_replace when str_replace is much easier.
$str = str_replace('\\', '', $str);
To use backslash in replacement, it must be doubled (\\\\ PHP string) in preg_replace
echo preg_replace('/\\\\/', '', $var);
You can also use stripslashes() like,
<?php echo stripslashes($var); ?>
$str = preg_replace('/\\\\(.?)/', '$1', $str);
This worked for me!!
preg_replace("/\//", "", $input_lines);

replace string (variables) php

I have a string like this: foo($bar1, $bar2)
How to I replace each variable with <span>$variable</span> with regexp?
This is my try (not working):
$row['name'] = preg_replace("/\$\w+/S", "<span>$1</span>", $row['name']);
I only want the variables to be replaced and have a span around them, I don't want commas or spaces to be replaced.
What I want is to have my string foo($bar1, $bar2) to be replaced with foo(<span>$bar1</span>, <span>$bar2</span>) ($bar1 and $bar2 are not variables, it's plain text).
Here are some problems I can see:
Since you are using double quotes for the regex,
you need to escape the $ using two \ as \\$.
Alternatively you can just use single
quote and use \$.
You are using $1 in the replacement
but you are not having any group in
the regex. So have ( ) around
\$\w+
So try:
$str = preg_replace('/(\$\w+)/', "<span>$1</span>", $str);
or
$str = preg_replace("/(\\$\w+)/", "<span>$1</span>", $str);
See it.

Categories