How to make a regular expression?

As from a string:
<a href="site.ru/page.php#">
to
<a href="site.ru/page.php" target="_blank">

Explanation: if in the url the last character is "#", then to clean it up and add the attribute target="_blank"
The link may contain a rel attribute such as rel="nofollow", it is necessary to leave.

Most importantly, links are searched for the regular expression: /<a[^>]+/.
Let me explain, the processing gets the string of the form:
<a href="site.ru/page.php#"
March 23rd 20 at 19:46
1 answer
March 23rd 20 at 19:48
Solution
in PHP, probably something like this:
preg_replace('/(<a[^#]+)#/', '\1" target="_blank', $str)

Clicking on the link, in SUBSTITUTION see the desired result. This is the replacement.
You in terms are you missing a quotation mark:
preg_replace('/(<a[^#>]+)#"/',

PS. And then maybe someone will need, and there's a mistake.
To check, you can enter a url="#bottom" and see the error. - alycia_Effer commented on March 23rd 20 at 19:51
@alycia_Effer, if you put a quotation mark, then the expression to replace the changes to \1" target="_blank"
I gave the link, there all is shown
If after grating writing something, then Yes, quotation mark will be stop. However, such nuances are not specified)
But then preg_match to do first, to identify links without adding, or make a different expression when conditions are more.
for example - johnson.Fritsch commented on March 23rd 20 at 19:54
@johnson.Fritsch, I have no complaints, I wrote, you never know who will need it.

Thanks for the solution. - alycia_Effer commented on March 23rd 20 at 19:57
And You have even ignored 2 quotes, one here:
target="_blank', $str)
need
target="_blank"', $str) - alycia_Effer commented on March 23rd 20 at 20:00
@alycia_Effer, I wrote about it, if you put kavukcu here, the expression for the replacement, tobish \1" target="_blankchanged to \1" target="_blank"
For if we are looking for a quote, then change with the quote, so in result it will not, so you need to add. And I have implemented search to quotes, without touching it, so it is added automatically.
Should play on regexp101) - johnson.Fritsch commented on March 23rd 20 at 20:03
@johnson.Fritsch, tell me more please, how can I check, does?
$link="<a href='https://mail.ru/page.php'";
if(!preg_match('/(<a[^#>]+)#"/', $link)) echo "Nope";


and the #bottom of the app as well?
!preg_match('/%href="#/', $link) - alycia_Effer commented on March 23rd 20 at 20:06
@alycia_Effer, preg_match and so generates a Boolean value.
var_dump(preg_match('/(<a[^#>]+)#"/', $link)); - johnson.Fritsch commented on March 23rd 20 at 20:09
@alycia_Effer, not true, I would advise first on this service to make. No selection - no coincidence)
There is no grid and search grid goes. - johnson.Fritsch commented on March 23rd 20 at 20:12
@alycia_Effer, !preg_match('/href="#/', $link);
all the anchors.
the percentage is not a special character in regexps, it is for other searches. - johnson.Fritsch commented on March 23rd 20 at 20:15
It is possible still a question? The last one, I promise ))
$site_link = "mail.ru";
$link='<a href="https://mail.ru/"';
echo preg_replace("%(href=\S(?!$site_link))%i", 'target="_blank" $1', $link);

Outputs:
<a target="_blank" href="https://mail.ru/"
How to:
<a href="https://mail.ru/" target="_blank" - alycia_Effer commented on March 23rd 20 at 20:18
@alycia_Effer,
$site_link = '/mail\.ru/';
$link='<a href="https://mail.ru/"';
if (preg_match($site_link, $link)) {
 echo $link .= 'target="_blank"';
}
- johnson.Fritsch commented on March 23rd 20 at 20:21
In wordpress
$site_link = get_bloginfo('url')
which equals: https://mail.ru or http://mail.ru
how from this to make $site_link = '/mail\.ru/';

You can certainly hands to write it, because the domain does not change, but as it is not flexible at it.
You can parse the URL:
$site_link = "https://mail.ru";
$parseUrl = parse_url($site_link);
echo "/". preg_replace('/\./', '\.', $parseUrl['host']). "/";

but IMHO the solution to some not? - alycia_Effer commented on March 23rd 20 at 20:24
Write like this:
$site_url = 'https://mail.ru/';
echo preg_replace('/(.*)(:\/\/)(.*)(\.)(.*)(\/)/i', "/$3\.$5/", $site_url);
XS is normal or not, but it seems to work - alycia_Effer commented on March 23rd 20 at 20:27
@alycia_Effer, I had to immediately write "is https://mail.ru/sometimes http://mail.ru/and instead of mail EN more .... And you need to determine if the link to IT, then add target"
And even on this
How to:
<a href="https://mail.ru/" target="_blank"
you can answer just echo $link .= 'target="_blank"';.
If you have dynamic data, the conditions - it's necessary to specify in the question that would not have happened, "not flexible" :)
And just have confidence in that as it is now - flexibly? Links are different, for example https://ru.mail.ru/page/page.php?rrwer=1&sfdsf=1or not in this case?) And here is the link https://mail.ru just will not find) No last slash, and looking for him. And with security there like?
And bracket groups don't need so many when only 2 to get it and there is no cancellation groups.
I would have written something like that, probably
echo preg_replace('/([\w-]+?)\.(\w{2,4}).*/', '/$1\.$2/', $site_url)'
but better to know what options there are.
In this case, the url only letters, numbers, underscores (\w), and dash, after only one point after 2 to 4 \w.
If you have subdomains, then you have the lazy search remove ([\w-\.]+) and screening point separately, and then already to do that under the regular season - johnson.Fritsch commented on March 23rd 20 at 20:30
You're right, I even didn't think about that.
If you take something like:
mail.ru
sub.mail.net

it's not working, what You have written echo issues:
https:///mail\.ru/
not
/mail\.ru/ - alycia_Effer commented on March 23rd 20 at 20:33
but understand, it is not necessary, it is possible to take a full domain with Protocol - alycia_Effer commented on March 23rd 20 at 20:36
then still not right, must be the same:
/https://mail\.ru/ - alycia_Effer commented on March 23rd 20 at 20:39
I can not understand how this is solved, then it turns out that we need 2 commands to use?
highlight domain/subdomain
and then replace . on \. ? - alycia_Effer commented on March 23rd 20 at 20:42
2 team looks like this:
$site_link = 'https://sub.mail.net/';
$mask = preg_replace('/.*\/\/([\w-\.]+)\/?.*/i', '/$1/', $site_link);
echo str_replace('.','\.', $mask);

and one can write or not? - alycia_Effer commented on March 23rd 20 at 20:45
@alycia_Effer, Yes, I https is not isolated and is not removed in this, had not thought of)
not worth it, now normally written - johnson.Fritsch commented on March 23rd 20 at 20:48
@johnson.Fritsch, faced with the problem on the main issue:
<a href="https://site.ru/page.php#bottomt#">
not working! - alycia_Effer commented on March 23rd 20 at 20:51
Wrote to (.*)# it seems to work, but just to be safe like better:
((w\.-)*)# - alycia_Effer commented on March 23rd 20 at 20:54
No didn't work, now work:
<a href="https://site.ru/page.php#bottom"> - alycia_Effer commented on March 23rd 20 at 20:57
And maybe it works ) - alycia_Effer commented on March 23rd 20 at 21:00
In regerx101 not correct, and the php correctly. You do not understand. - alycia_Effer commented on March 23rd 20 at 21:03
@alycia_Effer, what do I get? site.ru or site.ru/page.php ? - johnson.Fritsch commented on March 23rd 20 at 21:06
@alycia_Effer,
Wrote to (.*)# it seems to work, but just to be safe like better:
((w\.-)*)#

First, \w - shielded special character
Second, [\w\.-] - set []and not the bracket group
Thirdly (.*)# - gets everything except ">
Orops must be split into:
there are <a href="https://site.ru/page.php#bottomt#">
need ? - johnson.Fritsch commented on March 23rd 20 at 21:09
@johnson.Fritsch, You have in first post is written all right, on account of the example, i.e.:
<a href="https://site.ru/page.php#"> get <a href="https://site.ru/page.php" target="">
<a href="https://site.ru/page.php#bottom#"> get <a href="https://site.ru/page.php#bottom" target="">
- alycia_Effer commented on March 23rd 20 at 21:12
Today, some nonsense going on with toster, that authorization flies, the messages disappear.. - alycia_Effer commented on March 23rd 20 at 21:15
@alycia_Effer, not a bad problem work, well here try.
preg_replace('/(.*)#\s?(.*)"/', '\1\2" target="_blank"', $str)

or more possible variants write
messages I deleted not the right options wrote - johnson.Fritsch commented on March 23rd 20 at 21:18
5d0207e70e4b4233876675.png - alycia_Effer commented on March 23rd 20 at 21:21
I just don't quite understand what You did not accept my version ((w\.-)*)#
The meaning of which, any letters and numbers and dash meet 1 or more times. Perhaps there is still not enough # in the string.
Well, it all ends with #
After all, the main goal is to determine whether in the end # or not, and then remove it.

Left Your option, but still think it is a bit complicated and a more simple working. Left to Your option, as you didn't realized that I was not satisfied with regex101.
----
I mean the option:
(<a[^#]+)(#|.*)#" - alycia_Effer commented on March 23rd 20 at 21:24
@alycia_Effershorter page.php#bottom is not right then works determines that there is #
Here, I completed the responsive version:
preg_replace('/(.*)#"(\s?.*)/', '\1" target="_blank"\2', $str)

Now just everything works)
--
not satisfied with my version ((w\.-)*)#

wrote (w - ie just the letter " w " and not any letters+numbers+underscore (\w
and wrote in parentheses (\w\.-)* - ie strict sequence (letter+dot+dash)* (repeat).
and I [\w\.-]* ie any of the characters (letter/fine/dash)* (repeat).

and about safety, is it necessary? the user enters a tag or what? you can htmlspecialchars() to do the tags <> will be recognized as text. and check and clean, like if the object which they try to use, well, and :: then.

but if the text to parse, due to the fact that somewhere there is https: and somewhere in there, : it is necessary in a combination to drive.
about security I'm in a different job wrote where the website is need to get and the regular season to do so as there is a site there - that is the question.. If no user input, do not care.
the security issue should be solved when the user input. - johnson.Fritsch commented on March 23rd 20 at 21:27
like while works )) already afraid that again something wrong )) just kidding

for security is if someone in the comments of the website is not the correct url to enter it, but while for me it is not true

Thank you - alycia_Effer commented on March 23rd 20 at 21:30

Find more questions by tags PHP