PHP How to find string after Match/Wildcard/Match - php

Say I have the following string, $mytext:
[QUOTE=FirstUserRANDOMNUMBER]First user's post[/QUOTE]
Great post, FirstUser
[QUOTE=SecondUserRANDOMNUMBER]Second user's post[/QUOTE]
Awful post. I didn't like it.
Given a Username, how can I find the text below the quoted User? (ignoring RANDOMNUMBER)
I want to be able to input "SecondUser" into a function and have it return: "Awful post. I didn't like it."
Basically, I just want the response to the quoted user.
I thought I'd do a substr on $mytext based on the following:
strpos($username, $mytext) + strlen("[QUOTE=$username") + strlen("[/QUOTE]")
But I don't know how to get the length of the wildcard text in between [QUOTE=$username and the next instance of [/quote]
This must support the occurrence of multiple quotes in $mytext, like the example above.
Thanks!

You can use a regex like this:
\[QUOTE=SecondUser.*\[\/QUOTE\]([^[]*)
Working demo

Search for, but don't capture (within (...) groups) everything you want to exclude. In this case, \[Quote.*. Then search for (and capture) everything you DO want to keep. It will be saved in the \1 reference:
^\[QUOTE.*\n+|(^.*$)
It will match the [QUOTE...] line, but it doesn't capture that line: The only lines that are kept are
Great post, FirstUser
Awful post. I didn't like it.
Example

Related

PHP: Setting missing " in HTML-code with preg_replace?

I've got a database with a lot of user made entries grown about 10 years. The users had the option to put HTML-code in their content. And some didn't that well. So I've a lot of content in where the quotes are missing. Need a valid HTML-code for an ex/import via XML.
Had tested to replace width but my regex doesn't work. Do you've an idea where's my fault?
$out=preg_replace("/<a href=h(.)*>/","<a href=\"h$1\">",$out);
PS: If you have an idea how to automatically make a correction on wrong html source this would alternatively be great.
I think you wanted to use "/<a href=h(.*)>/" (mind the star inside the parenthesis) since you want to capture all characters after the h and before the > inside the capture group.
You can also use <a href=([^"].*)> since the href may not start with h. This regex captures all href values that do not start with ".
Yet, all of these assume that the href is the last attribute in your a, i.e.., ending with >.
As a more general rule, I came up with (?<key>\w*)\s*=\s*(?<value>[^"][^\s>]*) that finds attribute-value pairs, separated by =. The values may not start with ", and they go until the next whitespace or >. Use this with caution, since it may fail in serveral circumstances: Multi-line html, inline JavaScript, etc.
Whether it is a good idea to use RegEx for such a task is a different discussion.

PHP/ Markdown: Need to match code blocks inside backticks

I am creating a developers site with code views,
but when they type code i need to it a certain way so in the php and can convert it to html a special way,
The markdown i need to be able to work with is
```codechanger
``php
<?php ?>
``
``c#
$foo = bar;
``
```
So once the first regex select code changer I then need to be able to select each code but i just need to know the regex to match everything inside there even if it is on multiple lines.
This is what i was trying to use
preg_match_all("/\`\`codechanger.*?^\`\`[^\r\n]*/s", $text, $out);
Any questions feel free to ask.
If you want to get the text between a start and end point, you can do something like this: START .*? END
```codechanger(.*?)```
When combined with the s flag like you have, that will give you everything in $1 between the starting and ending positions.
Once you have the results you can do another preg_match_all to get the inner tags.
Here is a demo

Can't use OR( | ) in php Regular expression

I'm a newbie here. I'm facing a weird problem in using regex in PHP.
$result = "some very long long string with different kind of links";
$regex='/<.*?href.*?="(.*?net.*?)"/'; //this is the regex rule
preg_match_all($regex,$result,$parts);
Here in this code I'm trying to get the links from the result string. But it will provide me only those links which contains .net. But I also want to get those links which have .com. For this I tried this code
$regex='/<.*?href.*?="(.*?net|com.*?)"/';
But it shows nothing.
SOrry for my bad English.
Thanks in advance.
Update 1 :
now i'm using this
$regex='/<.*?href.*?="(.*?)"/';
this rule grab all the links from the string. But this is not perfect. Because it also grabs other substrings like "javascript".
The | character applies to everything within the capturing group, so (.*?net|com.*?) will match either .*?net or com.*?, I think what you want is (.*?(net|com).*?).
If you do not want the extra capturing group, you can use (.*?(?:net|com).*?).
You could also use (.*?net.*?|.*?com.*?), but this is not recommended because of the unnecessary repetition.
Your regex gets interpreted as .*?net or com.*?. You'll want (.*?(net|com).*?).
Try this:
$regex='/<.*?href.*?="(.*?\.(?:net|com)\b.*?)"/i';
or better:
$regex='/<a .*?href\s*+=\s*+"\K.*?\.(?:net|com)\b[^"]*+/i';
<.*?href
is a problem. This will match from the first < on the current line to the first href, regardless of whether they belong to the same tag.
Generally, it's unwise to try and parse HTML with regexes; if you absolutely insist on doing that, at least be a bit more specific (but still not perfect):
$regex='/<[^<>]*href[^<>=]*="(?:[^"]*(net|com)[^"]*)"/';

Php regex match a string between two html tags with the tags been unknown

Ok, so here's my issue:
I have a link, say: http://www.blablabla.com/watch?v=1lyu1KKwC74&feature=list_other&playnext=1&list=AL94UKMTqg-9CfMhPFKXPXcvJ_j65v7UuV
And the link is between two tags say like this:
<br>http://www.blablabla.com/watch?v=1lyu1KKwC74&feature=list_other&playnext=1&list=AL94UKMTqg-9CfMhPFKXPXcvJ_j65v7UuV<br></p>
Using this regex with preg_replace:
'#(^|[^\/]|[^>])('.addcslashes($link,'.?+').')([^\w\/]|[^<]$)#i'
As such:
preg_replace('#(^|[^\/]|[^>])('.addcslashes($link,'.?+').')([^\w\/]|[^<]$)#i', "***",$strText);
The resulted string is :
<br***p>
Which is wrong!!
It should have been
<br>***<br></p>
How can I get the desired result? I have blasted my head out trying to solve this one out.
I would like to mention that str_replace replaces even the link within another valid link, so it's not a good method, I need an exact match between two boundaries, even if the boundary is text or another HTML tag.
Assuming you don't want to use a DOM parser for some reason, I believe doing what you intended is as simple as the following:
preg_replace('#(^|[^\/]|[^>])('.addcslashes($link,'.?+').')([^\w\/]|[^<]$)#i', "$1***$3",$strText);
This uses $1 and $3 to put back the delimiting text you matched in your regular expression.
As others have pointed out, using a DOM parser is more reliable.
Does this do what you want?

PHP preg_replace_callback correct escaping to handle hash symbol ('#')

I'm doing some work with a Twitter feed and want to turn any hashtags into a clicable URL.
A hashtag is a hash symbol ('#') immediately followed by a word acting as a search tag - and contains no spaces.
An example would be ...
#Eutechnyx looking to form a tech group in #Shoreditch next year. Game and Web programmers get in touch. #AutoClubRev
There are two tags here, #Shoreditch and #AutoClubRev.
These should respectively become the following links ...
https://twitter.com/#!/search?q=%23Shoreditch
and
https://twitter.com/#!/search?q=%23AutoClubRev
I'm assuming I should be using preg_replace_callback here and not just vanilla preg_replace, as I am trying to take a backreference ($1) and change it not just display it. But of course I could be wrong. I'm not fuessed on which function to use - as long as it does the job and is relatively efficient.
Thanks,
Pete
preg_replace should be able to do it.
$test = "#Eutechnyx looking to form a tech group in #Shoreditch next year. Game and Web programmers get in touch. #AutoClubRev";
echo preg_replace('|#([\w_\d]+)|', '#\1', $test);

Categories