This question already has an answer here:
PHP regex. Convert [text](url) to text [duplicate]
(1 answer)
Closed 3 years ago.
I have 1000s of posts in Wordpress that have this weird code for a hyperlink in the body copy. For example, I want to find all instances of this:
[Website Name](http://www.website.com)
and turn it into
Website Name
What is the best way to achieve this in php?
$string = "This is a blog post hey check out this website [Website Name](http://www.website.com). It is a real good domain.
// do some magic
You can use preg_replace with this regex:
\[([^]]+)]\((http[^)]+)\)
It looks for a [, followed by some non-] characters, a ] and (http, then some non-) characters until a ).
This is then replaced with $1. For example:
$string = "This is a blog post hey check out this website [Website Name](http://www.website.com). It is a real good domain.";
echo preg_replace('/\[([^]]+)]\((http[^)]+)\)/', '$1', $string);
Output:
This is a blog post hey check out this website Website Name. It is a real good domain.
This weird code is Markdown (used for example here in SO).
If you want to convert it to HTML using PHP you could use this library : https://parsedown.org/
The advantage is that you will convert any other markdown tags and other forms of markdown links present in the posts.
Related
This question already has answers here:
PHP strtr vs str_replace benchmarking
(3 answers)
Replace text in a string using PHP
(3 answers)
Closed 5 years ago.
I have a PHP string in which I would like to find and replace using the strtr function, problem is I have variable fields so I won't be able to replace by name. The string contains tags like the following:
[field_1=Company]
[field_4=Name]
What makes it difficult is the "Company" and "Name" part of the "tag", these can be variable. So I basically looking for a way to replace this part [field_1] where "=Company" and "=Name" must be discarded. Can this be done?
To explain: I'm using "=Company" so users don't just see "field_1" but know the value it represents. However users are able to change the value to what they see fit.
You are probably looking for regular expressions. There is a function in PHP to do a regex replace:
http://php.net/manual/en/function.preg-replace.php
Been a while since I've worked in PHP but you might want to try something like this:
preg_replace('/field_\d/','REPLACEMENT','[field_1=Company]');
Should result in
[REPLACEMENT=Company]
If you want to replace everything except the brackets:
preg_replace('/field_\d+=\w+/','REPLACEMENT','[field_1=Company]');
This question already has answers here:
Replace URLs in text with HTML links
(17 answers)
Closed 8 years ago.
How can I detect URL in text area that doesn't include http://? Here is an a example for a input:
Hi bro! Look at my new website: www.example.com. I leard to build websites from http://another.example.net and from example.net.
Is there a way to convert it to this code?:
Hi bro! Look at my new website: www.example.com.
I leard to build websites from http://another.example.net
and from example.net.
As you can see, the code detects if there are a URL even if it doesn't starts with http:// or www, and adds to the a tag the http://.
See this basic example. It allows you to match in multi-line texts and it is not too restrictive. You could have links to internal network, where the machine hostname is used like: http://myspecialserver - this is valid link, no matter it might be accessible only by certain network(s).
The anwser uses the regular expressions. You can read more about them here: http://www.tutorialspoint.com/php/php_regular_expression.htm
We match with them the protocol and any text after which is consistent for URL, it does not contain space charaters, tabs, carriage returns and line feeds.
<?php
function linkify($text) {
return preg_replace('#\b(http|ftp)(s)?\://([^ \s\t\r\n]+?)([\s\t\r\n])+#smui', '$1$2://$3$4', $text);
}
echo nl2br(linkify('
Hello, visit https://www.domain.com
We are not partners of http://microsoft.com/ :)
Download source from: ftp://new.sourceforge.com
'));
?>
This question already has answers here:
Regular Expression for extracting text from an RTF string
(11 answers)
Closed 9 years ago.
A column in the database I work with contains RTF strings, I would like to strip these out using PHP, leaving just the sentence between.
It is a MS SQL database 2005 if I recall correctly.
An example of the kind of strings pulled from the database (need any more let me know, all the rest are similar):
{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fnil\fcharset0 Tahoma;}}
\viewkind4\uc1\pard\lang1033\f0\fs17 ASSEMBLE COMPONENTS AS DETAILED ON DRAWING.\lang2057\fs17\par
}
I would like this to be stripped to only return:
ASSEMBLE COMPONENTS AS DETAILED ON DRAWING.
Now, I have successfully managed to strip the characters in ASP.NET for a previous project, however I would like to do so using PHP. Here is the regular expression I used in ASP.NET, which works flawlessly may I add:
"(\{.*\})|}|(\\\S+)"
However when I try to use the same expression in PHP with a preg_replace it does not strip half of the characters.
Any regex gurus out there?
Use this code. it will work fine.
$string = preg_replace("/(\{.*\})|}|(\\\S+)/", "", $string);
Note that I added a '/' in the beginning and at the end '/' in the regex.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove urls using PHP
I'm trying to figure out the best way to remove URLs from text with php. I've looked at a bunch of different sites and questions on here but can't quite piece it all together.
I would like to remove all URLs like the following:
www.website.com
http://www.website.com
website.com
website.com/test
<tag>www.website.com</tag> (where <tag> is any html tag)
(www.website.com)
I've tried a few solutions i found on here, but i couldn't figure out how to get web addresses with bordering characters not part of the web address, i.e. parenthesis or within an HTML tag like <strong>
Any help is much appreciated.
Thanks
Perhaps...
// http(s)://
$txt = preg_replace('|https?://www\.[a-z\.0-9]+|i', '', $txt);
// only www.
$txt = preg_replace('|www\.[a-z\.0-9]+|i', '', $txt);
Or:
$Var = str_replace("itemtoreplace", "replacewith", $variabletoremovefrom");
PHP Str_replace
Use:
Remove urls using PHP
For reference.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Identifying if a URL is present in a string
Php parse links/emails
I'm working on some PHP code which takes input from various sources and needs to find the URLs and save them somewhere. The kind of input that needs to be handled is as follows:
http://www.youtube.com/watch?v=IY2j_GPIqRA
Try google: http://google.com! (note exclamation mark is not part of the URL)
Is http://somesite.com/ down for anyone else?
Output:
http://www.youtube.com/watch?v=IY2j_GPIqRA
http://google.com
http://somesite.com/
I've already borrowed one regular expression from the internet which works, but unfortunately wipes the query string out - not good!
Any help putting together a regular expression, or perhaps another solution to this problem, would be appreciated.
Jan Goyvaerts, Regex Guru, has addressed this issue in his blog. There are quite a few caveats, for example extracting URLs inside parentheses correctly. What you need exactly depends on the "quality" of your input data.
For the examples you provided, \b(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&##/%=~_|$?!:,.]*[A-Z0-9+&##/%=~_|$] works when used in case-insensitive mode.
So to find all matches in a multiline string, use
preg_match_all('/\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&##\/%=~_|$?!:,.]*[A-Z0-9+&##\/%=~_|$]/i', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];
Why not try this one. It is the first result of Googling "URL regular expression".
((https?|ftp|gopher|telnet|file|notes|ms-help):((\/\/)|(\\\\))+[\w\d:##%\/;$()~_?\+-=\\\.&]*)
Not PHP, but it should work, I just slightly modified it by escaping forward slashes.
source