preg-replace

How to replace spaces with %20 in <img> tags

可紊 提交于 2021-02-19 04:40:06
问题 I would like to replace all spaces in the image tags of a html text. Example: <img src="photo 1.jpg" /> to <img src="photo%201.jpg"/> I didn't find a soultion with preg_replace, but it may be a simple regexp line. Thanks! Edit : Sorry guys, my description was not very clear. So, I have a full html page and I only want to replace inside the img tags. I can't use urlencode here as it encodes the other stuff as well. 回答1: The space is represented by a %20 in the url but there are other chars

PHP preg_replace: Replace all anchor tags in text with their href value with Regex

拥有回忆 提交于 2021-02-17 02:31:42
问题 I want to replace all anchor tags within a text with their href value, but my pattern does not work right. $str = 'This is a text with multiple anchor tags. This is the first one: <a href="https://www.link1.com/" title="Link 1">Link 1</a> and this one the second: <a href="https://www.link2.com/" title="Link 2">Link 2</a> after that a lot of other text. And here the 3rd one: <a href="https://www.link3.com/" title="Link 3">Link 3</a> Some other text.'; $test = preg_replace("/<a\s.+href=['|\"]([

PHP preg_replace: Replace all anchor tags in text with their href value with Regex

痴心易碎 提交于 2021-02-17 02:31:34
问题 I want to replace all anchor tags within a text with their href value, but my pattern does not work right. $str = 'This is a text with multiple anchor tags. This is the first one: <a href="https://www.link1.com/" title="Link 1">Link 1</a> and this one the second: <a href="https://www.link2.com/" title="Link 2">Link 2</a> after that a lot of other text. And here the 3rd one: <a href="https://www.link3.com/" title="Link 3">Link 3</a> Some other text.'; $test = preg_replace("/<a\s.+href=['|\"]([

PHP preg_replace: Replace all anchor tags in text with their href value with Regex

▼魔方 西西 提交于 2021-02-17 02:31:09
问题 I want to replace all anchor tags within a text with their href value, but my pattern does not work right. $str = 'This is a text with multiple anchor tags. This is the first one: <a href="https://www.link1.com/" title="Link 1">Link 1</a> and this one the second: <a href="https://www.link2.com/" title="Link 2">Link 2</a> after that a lot of other text. And here the 3rd one: <a href="https://www.link3.com/" title="Link 3">Link 3</a> Some other text.'; $test = preg_replace("/<a\s.+href=['|\"]([

Replace comma or whitespace with hyphen in same string

心已入冬 提交于 2021-02-16 13:58:07
问题 I need PHP code to replace comma or whitespace with hyphen For eg: If $value = 'home garden' or $value = 'home,garden' , i need result as home-garden I tried $result = preg_replace('/\s+[\,]/', '-', trim($value)); , but no use.. Can someone explain it? 回答1: $result = preg_replace('/[ ,]+/', '-', trim($value)); Test: $value = ' home ,garden , gardener '; $result = preg_replace('/[ ,]+/', '-', trim($value)); echo $result; //home-garden-gardener 回答2: $result = str_replace(array(',', ' '), '-',

Regex for matching a tag in a Liquid template : “>” inside html tag

寵の児 提交于 2021-02-10 06:49:07
问题 I have to write a match pattern for the body tag in a Liquid template. While matching HTML tags is pretty straightforward, I have the problem that HTML special character can be used inside the Liquid code. Example: <body class="template-{{ template | replace: '.', ' ' | truncatewords: 1, '' }}{% if promo %}has-promo{% endif %} {% if products.size > 1 %}has-related-products{% endif %} {% if settings.product-hover == 'quick-shop' %}has-quick-shop{% endif %} loading" > or simplified: <body {%

PHP using preg_match or regex as value for array_search or the key for array_keys_exist

拈花ヽ惹草 提交于 2021-02-08 20:53:29
问题 I was wondering if it was possible to use regex or preg_match() in array_seach() or array_keys_exist ? ie. array_keys_exist($array,"^\d+$") to match all keys that are solely numeric characters 回答1: I don't know whether it suits your needs exactly, but you should have a look at the preg_grep function, which will check an array of strings against a regex and return all matching array elements. You could do same with the keys, by using preg_grep on the return value of array_keys. This is

How can i use php's preg_replace with a simple string and wildcards?

巧了我就是萌 提交于 2021-02-07 17:24:53
问题 If i have the string [link="*"] where * is a wildcard how could i then use php to replace the string with <a href="*"> where * is the same value as before? Is preg_replace the best way to do this? Thanks, any help appreciated! 回答1: preg_replace('~\[link="(.*?)"\]~', '<a href="$1">', $text); 回答2: $link = '[link="http://www.google.com/"]'; $link = preg_replace('/\[link="(.*)"\]/', '<a href="$1">', $link); 来源: https://stackoverflow.com/questions/7050152/how-can-i-use-phps-preg-replace-with-a

What does the regex pattern 'pL' do? [duplicate]

心已入冬 提交于 2021-02-07 07:22:20
问题 This question already has answers here : What is the {L} Unicode category? (2 answers) Closed 2 years ago . There is a common Regex used to slugify urls ~[^\\pL\d]+~u but what does the \\pL in the first preg_replace() mean? Here are some examples: How can I replace ":" with "/" in slugify function? http://snipplr.com/view/22741/slugify-a-string-in-php/ http://sourcecookbook.com/en/recipes/8/function-to-slugify-strings-in-php http://www.daniweb.com/web-development/php/threads/471380/php

Regex to replace http AND https links

点点圈 提交于 2021-02-07 04:40:32
问题 I have the following preg_replace() function targeting links: $link = preg_replace( "#http://([\S]+?)#Uis", '<a href="http://\\1">(link)</a>', $link ); It works fine with http links but obviously not with https links. How can I adjust it so that it works with https links as well. 回答1: Just add s? after http and match the whole link, then use the $0 backreference to refer to it from the replacement pattern: $link = preg_replace( "#https?://\S+#i", '<a href="$0">(link)</a>', $link ); See the