PHP how can i remove substring between last slashes? - php

Here is my string:
test-e2e4/folder1/folder2/6.png.
I want to remove the last part of it, so it should look like this:
test-e2e4/folder1/folder2/
How can i do that using preg_replace() ?

$string = preg_replace('#[^/]*$#', '', $string);
DEMO.

besides preg_replace, you can also use dirname
echo dirname("test-e2e4/folder1/folder2/6.png") . "/";

Related

how to remove everything before second occurance of underscore

I couldn't find the solution using search.
I am looking for a php solution to remove all character BEFORE the second occurance of and underscore (including the underscore)
For example:
this_is_a_test
Should output as:
a_test
I currently have this code but it will remove everything after the first occurance:
preg_replace('/^[^_]*.s*/', '$1', 'this_is_a_test');
Using a slightly different approach,
$s='this_is_a_test';
echo implode('_', array_slice( explode( '_', $s ),2 ) );
/* outputs */
a_test
preg_replace('/^.*_.*_(.*)$/U', '$1', 'this_is_a_test');
Note the U modifier which tells regex to take as less characters for .* as possible.
You can also use explode, implode along with array_splice like as
$str = "this_is_a_test";
echo implode('_',array_splice(explode('_',$str),2));//a_test
Demo
Why go the complicated way? This is a suggestion though using strrpos and substr:
<?php
$str = "this_is_a_test";
$str_pos = strrpos($str, "_");
echo substr($str, $str_pos-1);
?>
Try this one.
<?php
$string = 'this_is_a_test';
$explode = explode('_', $string, 3);
echo $explode[2];
?>
Demo
I'm still in favor of a regular expression in this case:
preg_replace('/^.*?_.*?_/', '', 'this_is_a_test');
Or (which looks more complex here but is easily adjustable to N..M underscores):
preg_replace('/^(?:.*?_){2}/', '', 'this_is_a_test');
The use of the question mark in .*? makes the match non-greedy; and the pattern has been expanded from the original post to "match up through" the second underscore.
Since the goal is to remove text the matched portion is simply replaced with an empty string - there is no need for a capture group or to use such as the replacement value.
If the input doesn't include two underscores then nothing is removed; such can be adjusted, very easily with the second regular expression, if the rules are further clarified.

PHP Remove spaces and %20 within single function

I wish to remove white space from a string. The string would have ben urlencoded() prior, so I also wish to remove %20 too. I can do this using two separate functions, but how do i do this with one function?
$string = str_replace("%20","",$string);
$string = str_replace(" ","",$string);
You could use preg_replace function.
preg_replace('~%20| ~', "", $string)
Don't use a regex for that but strtr:
$result = strtr($str, array('%20'=>'', ' '=>''));

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

how to remove last occurance of underscore in string

I have a string that contains many underscores followed by words ex: "Field_4_txtbox" I need to find the last underscore in the string and remove everything following it(including the "_"), so it would return to me "Field_4" but I need this to work for different length ending strings. So I can't just trim a fixed length.
I know I can do an If statement that checks for certain endings like
if(strstr($key,'chkbox')) {
$string= rtrim($key, '_chkbox');
}
but I would like to do this in one go with a regex pattern, how can I accomplish this?
The matching regex would be:
/_[^_]*$/
Just replace that with '':
preg_replace( '/_[^_]*$/', '', your_string );
There is no need to use an extremly costly regex, a simple strrpos() would do the job:
$string=substr($key,0,strrpos($key,"_"));
strrpos — Find the position of the last occurrence of a substring in a string
You can also just use explode():
$string = 'Field_4_txtbox';
$temp = explode('_', strrev($string), 2);
$string = strrev($temp[1]);
echo $string;
As of PHP 5.4+
$string = 'Field_4_txtbox';
$string = strrev(explode('_', strrev($string), 2)[1]);
echo $string;

Matching Word with regular expression

I have the following text file:
...
"somewords MYWORD";123123123123
"someother MYWORDOTHER";456456456456
"somedifferent MYWORDDIFFERENT";789789789
...
I need to match the word MYWORD, MYWORDOTHER, MYWORDDIFFERENT and then substitute the space before this word with ";".
Someone can figure out a regex?
I have done something like that:
+[^ ][^ ][^ ][^ ][^ ][^ ][^ ]";
but this works only with a specific word length. I need to modify to get any word of any length.
Any help?
why don't you str_replace() ?
$string = '"somewords MYWORD";123123123123
"someother MYWORDOTHER";456456456456
"somedifferent MYWORDDIFFERENT";789789789';
$replace = str_replace(' MYWORD', ';MYWORD', $string);
echo $replace;
Codepad Example
This is untested, but should work to replace the space before the last word in the quotes...
preg_replace('/(".+) (\w+";\d+)/',"$1;$2", $your_string);
preg_replace('/\s(\w+);\d/', ';$1', $text);
Maybe this:
$result = preg_replace('/([ ])(\w+)";/im', ';$2";', $subject);
in:
"somewords MYWORD";123123123123
"someother MYWORDOTHER";456456456456
"somedifferent MYWORDDIFFERENT";789789789
out:
"somewords;MYWORD";123123123123
"someother;MYWORDOTHER";456456456456
"somedifferent;MYWORDDIFFERENT";789789789
Use this :
preg_replace('#"(\w+)\s+(\w+)"#',"$1;$2",$text);
while($line=fgets($file))
{
$str=preg_replace("/ (\w)/i",";$1",$line);//use this line if you want to replace every space
$str=preg_replace("/ (\w+)\";(\d)/i",";$1\";$2",$line);//use this line if you only want to replace the last space
echo $str;//or wherever you want to output
}
Edit:
Alright, I made a typo in the original answer.
Now corrected with a codepad:http://codepad.org/leQHTuFR

Categories