Matching Word with regular expression - php

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

Related

Change ocurrence inside a string

I am needing to change occurrence of string or numbers in php. In this situation, I need to change this, if it happens:
[code:154545edppy]
// my code here
[/code]
to this
[code]
// my code here
[/code]
I need to verify if letters and strings appear inside de opening block code. I am trying to do this with str_replace, but it's not working.
my code now:
$text = "[code:54as] [/code]";
$text = str_replace("[code: {(\d)}{(\w)}]", "[code]", $text);
$text = str_replace("[/code: {(\d)}{(\w)}]", "[/code]", $text);
echo $text;
str_replace is static. Use preg_replace with a regex and you can accomplish your task.
Something like:
$text = "[code:54as] [/code]";
echo preg_replace('~(\[/?.*?):.*?\]~', '$1]', $text);
Should do it.
PHP Demo: https://eval.in/643544
Regex demo: https://regex101.com/r/mD1bM3/1
If you only want to replace numbers and letters after the : use a character class in place of the second .*?. [A-Za-z\d]*?.

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 how can i remove substring between last slashes?

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") . "/";

How to remove dots from this combination in PHP

I have this combination in a string:
"I am tagging #username1.blah. and #username2.test. and #username3."
I tried this:
preg_replace('/\#^|(\.+)/', '', 'I am tagging #username1.blah. and #username2.test. and #username3. in my status.');
But the result is:
"I am tagging #username1blah and #username2test and #username3 in my status"
The above result is not what I wanted.
This is what I want to achieve:
"I am tagging #username.blah and #username2.test and #username3 in my status."
Could someone help me what I have done wrong in the pattern?
Many thanks,
Jon
I don't like regex very much, but when you are sure that the dots you want to remove are always followed by a space, you could do something like this:
php > $a = "I am tagging #username1.blah. and #username2.test. and #username3.";
php > echo str_replace(". ", " ", $a." ");
I am tagging #username1.blah and #username2.test and #username3
Try this:
preg_replace('/\.(\s+|$)/', '\1', $r);
This will replace dots at the end of "words" that are starting with #
$input = "I am tagging #username1.blah. and #username2.test. and #username3. in my status.";
echo preg_replace('/(#\S+)\.(?=\s|$)/', '$1', $input);
(#\S+)\.(?=\s|$) will match a dot at the end of a non whitespace (\S) series when the dot is followed by whitespace or the end of the string ((?=\s|$))
preg_replace('/\.( |$)/', '\1', $string);
How about:
preg_replace("/(#\S+)\.(?:\s|$)/", "$1", $string);
/\#\w+(\.\w+)?(?<dot>\.)/
That will match all dots and name them in the dot group

Terrible with regex

How can I make this regular expression replace spaces as well any non latin alpha numeric character?
preg_replace('/[^a-zA-Z0-9\s]/', '', $title)
Thanks a lot
[^...] matches anything but ....
\s matches spaces.
You don't want it to not match spaces.
Everything looks ok, you just have to assing it to a variable!
$title = preg_replace('/[^a-zA-Z0-9\s]/', '', $title)
I would just do:
<?php
preg_match_all('/[a-zA-Z0-9\s]/', $title, $out);
$ntitle = implode($out,'');
?>
EDIT: Briedis is right though, your regex works fine.

Categories