I have a string:
[startstring]hello = guys[endstring] hello guys [startstring]jerk = you[endstring] man this is good!!!
I would like remove everything in between [startstring] and [endstring], all the matches, and also remove the [startstring] and [endstring] from the string. So like the result would be:
hello guys man this is good!!!
and the deleted stuff would be:
[startstring]hello = guys[endstring] hello guys [startstring]jerk = you[endstring] man this is good!!!
See what I mean?
I would like to echo the resulted stuff, without the [startstring] and [endstring] stuff, as shown above. :)
Hope I'm mostly clear. :-)
How would I go about accomplishing this in PHP? Would this envolve some sort of RegEx? Could you provide a sample code?
Thanks so much in advance! :-)
Edit: Could this code be modified or be used somewhat to complete the task?
preg_match_all('/\[startstring\](.*?)\[endstring\]/s', $input, $matches);
To achieve that you just need to change your method name, you should use preg_replace instead preg_match_all:
preg_replace ($pattern, $replacement, $subject)
Searches subject for matches to pattern and replaces them with replacement.
Code
$input = "[startstring]hello = guys[endstring] hello guys [startstring]jerk = you[endstring] man this is good!!!";
$output = preg_replace('/\[startstring\](.*?)\[endstring\]/s', '', $input);
Output
hello guys man this is good!!!
Related
I've seen many answers, but none seem to help me with my situation. I need to extract the comments from this text:
Comments: These are the comments and they end at this full stop. Remember, you can only use the personal information...
OR this text:
Comments: These are the comments and they end at this full stop. You can only use the personal information...
A working example is here: https://regex101.com/r/apIT0O/1
But I can't seem to get this into PHP. I can get one to work with:
$pattern = "/(?<=Comments: )(.*?)(?= Remember, you can)/s";
But what is wrong with this?
$pattern = "/(?<=Comments: )(.*?)(?= Remember, you can)|(?<=Comments: )(.*?)(?= You can)/s";
Many thanks!
With preg_match_all you can get all result with your regex.
Try this:
/(?<=Comments: )(.*?)(?= Remember, you can)|(?<=Comments: )(.*?)(?= You can)/s
Explanation
Sample Code:
<?php
$re = '/(?<=Comments: )(.*?).(?= Remember, you can)|(?<=Comments: )(.*?)(?= You can)/sm';
$str = 'Comments: These are the comments and they end at this full stop. Remember, you can only use the personal information...
Comments: These are the comments and they end at this full stop. You can only use the personal information...';
preg_match_all($re, $str, $matches);
print_r($matches);
?>
Runt it here
You can get your desire value also by preg_match like this. Here is code
<?php
$data="Comments: These are the comments and they end at this full stop. Remember, you can only use the personal information...";
preg_match("/(.*)(?<=Comments:)(.*?)(?= Remember, you can)(.*)/",$data,$m);
echo $m[2];
?>
Output:
These are the comments and they end at this full stop.
Or you can use
print_r($m) it will print all the value you capture with regex.
Hope it helps you.
i have a string as follows:
$product_req = "CATEGORY-ACTIVE-8,CATEGORY-ACTIVE-4,ACTIVE-6,ACTIVE-9";
and i need a function that returns only the numbers preceded by "CATEGORY-ACTIVE-" (without the quotes) so in other words it should return: 8,4 and leave everything else out.
Is there any php function that can do this?
Thank you.
Use Preg_match_all and extract the first match
$input_lines="CATEGORY-ACTIVE-8,CATEGORY-ACTIVE-4,ACTIVE-6,ACTIVE-9"
preg_match_all("/CATEGORY-ACTIVE-(\d+)/", $input_lines, $output_array);
print_r(join(',',$output_array[1]));
output
8,4
Is there any php function that can do this?
Yes you can play around and achieve it with PHP Native functions by writing some code logic. But do it with Regular Expressions (to keep it simple and short).
Using PHP Functions..
<?php
$str = 'CATEGORY-ACTIVE-8,CATEGORY-ACTIVE-4,ACTIVE-6,ACTIVE-9';
$str=explode(',',$str);
$temparr=array();
foreach($str as $v)
{
if(strpos($v,'CATEGORY-ACTIVE-')!==false)
{
$temparr[]=str_replace('CATEGORY-ACTIVE-','',$v);
}
}
echo implode(',',$temparr); //"prints" 8,4
Use regular expressions and implode it atlast (Preferred way..)
<?php
$str = 'CATEGORY-ACTIVE-8,CATEGORY-ACTIVE-4,ACTIVE-6,ACTIVE-9';
preg_match_all('/CATEGORY-ACTIVE-(.*?),/', $str, $matches);
echo implode(',',$matches[1]); //8,4
I'd use a lookaround assertion to accomplish this:
(?<=CATEGORY-ACTIVE-)(\d+)
Visualization:
Code:
$str = 'CATEGORY-ACTIVE-8,CATEGORY-ACTIVE-4,ACTIVE-6,ACTIVE-9';
preg_match_all('/(?<=CATEGORY-ACTIVE-)(\d+)/', $str, $matches);
print_r($matches[1]);
Output:
Array
(
[0] => 8
[1] => 4
)
Demo
Yes there is, Feel free to explore the wonderful world of Regex!
http://il1.php.net/preg_match
I recommend you do a bit of reading on this yourself as "getting the answers" when it comes to regex is a sin, You learn nothing from it.
I'm not uber experienced with it myself, but it's one of those things that you must learn 'hands on', theory won't cut it here.
in theory it would look like this
$str = 84838493849384938;
preg_match_all(/[8.4]/, $str);
You can also go play around with REgex at this site http://www.phpliveregex.com/
What I'm trying to do here is take a string that might have what would normally be a code comment, and replace that with something else, especially wrapping it in something else. I'm pretty sure the preg_replace function would work here, but I don't have an idea on where to start with the Regex. For example:
Hello world //this is a comment
Testing //not testing
Test again
Would turn into
Hello world %//this is a comment%
Testing %//not testing%
Test again
preg_replace('???', '%$1%', $matches); is as much as I can figure out on my own, any help is much appreciated!
preg_replace('~//.*$~m', '', $str);
This will remove everything after (and including) // to the end of the line
http://ideone.com/Xhmpd
preg_replace('~//.*$~m', 'foo \\0 bar', $str);
This will wrap them with foo bar around
http://ideone.com/IqkWM
Try this:
$string = "Hello world //this is a comment";
preg_replace('/\/\/.*/', '%$0%', $string);
i am designing a site with a comment system and i would like a twitter like reply system.
The if the user puts #a_registered_username i would like it to become a link to the user's profile.
i think preg_replace is the function needed for this.
$ALL_USERS_ROW *['USERNAME'] is the database query array for all the users and ['USERNAME'] is the username row.
$content is the comment containing the #username
i think this should not be very hard to solve for someone who is good at php.
Does anybody have any idea how to do it?
$content = preg_replace( "/\b#(\w+)\b/", "http://twitter.com/$1", $content );
should work, but I can't get the word boundary matches to work in my test ... maybe dependent on the regex library used in versions of PHP
$content = preg_replace( "/(^|\W)#(\w+)(\W|$)/", "$1http://twitter.com/$2$3", $content );
is tested and does work
You want it to go through the text and get it, here is a good starting point:
$txt='this is some text #seanja';
$re1='.*?'; # Non-greedy match on filler
$re2='(#)'; # Any Single Character 1
$re3='((?:[a-z][a-z]+))'; # Word 1
if ($c=preg_match_all ("/".$re1.$re2.$re3."/is", $txt, $matches))
{
$c1=$matches[1][0];
$word1=$matches[2][0]; //this is the one you want to replace with a link
print "($c1) ($word1) \n";
}
Generated with:
http://www.txt2re.com/index-php.php3?s=this%20is%20some%20text%20#seanja&-40&1
[edit]
Actually, if you go here ( http://www.gskinner.com/RegExr/ ), and search for twitter in the community tab on the right, you will find a couple of really good solutions for this exact problem:
$mystring = 'hello #seanja #bilbobaggins sean#test.com and #slartibartfast';
$regex = '/(?<=#)((\w+))(\s)/g';
$replace = '$1$3';
preg_replace($regex, $replace, $myString);
$str = preg_replace('~(?<!\w)#(\w+)\b~', 'http://twitter.com/$1', $str);
Does not match emails. Does not match any spaces around it.
I am learning regex. I have a very simple question:
I have a long string of content in php.I would like to convert all places where it says:
http://www.example.com/en/rest-of-url
to
http://en.example.com/rest-of-url
Could somebody help me with this? I think I use preg_replace for this?
Bonus: If you have a link to a good site which explains how to do the simplest things like this in regex, please post it. Every regex resource I look at gets very complicated very fast (even the Wikipedia article).
In PHP:
$search = '~http://www.example.com/([^/]+)/(.+)~';
$replace = 'http://$1.example.com/$2';
$new = preg_replace( $search, $replace, $original );
http://regexlib.com/
has a nice regular expression cheat sheet and tester
Assuming:
preg_replace($regex, $replaceWith, $subject);
$subject is the original text. $regex should be:
'#http://([^\.]*)\.example\.com/en/(.*)#'
$replaceWith should be:
'http://$1.example.com/$2'
EDITED: In my orignial answer, I had missed the fact that you wanted to capture part of the domain name.
This will work with any domainname:
$url = 'http://www.example.com/en/rest-of-url';
echo preg_replace('%www(\..*?/)(\w+)/%', '\2\1', $url);
gives:
http://en.example.com/rest-of-url
Reference: preg_replace
You can learn about basic regex, however for your simple question, there's no need for regex.
$str="http://www.example.com/en/rest-of-url";
$s = explode("/",$str);
unset( $s[3]);
print_r( implode("/",$s) ) ;
This is a great site for Regex Tutorials
http://www.regular-expressions.info/
Regex Tutor