简体   繁体   中英

How to use PHP preg_replace and Javascript to replace 'link format' with a link tag

I want to allow links in a blog comments field but only if they are written in the following format (like reddit):

[reddit!](http://reddit.com)

And I want to turn them into a real link with PHP and with JavaScript:

<a href="http://reddit.com">reddit!</a>

I will use the JavaScript conversion to show a live preview on the page and the PHP conversion as server-side validation.

I don't need to add a rel="nofollow" to the link but I will need to add a http:// if one is omitted.

I know lots of websites allow for this kind of linking (like Stackoverflow) so aret there any code samples available anywhere?

You effectively want a PHP markdown parser: http://michelf.com/projects/php-markdown/

Here is a different project with a code example of how to parse the syntax you requested: https://github.com/wolfie/php-markdown/blob/extra/markdown.php#L678

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM