preg-replace

Regular expression matching A, B, and AB

僤鯓⒐⒋嵵緔 提交于 2020-01-02 08:03:40
问题 I would like to create a regular expression that matches A , B , and AB , where A and B are quite complex regular expressions. One solution is to use (A|A?B) or (AB?|B) , but then I have to repeat one of the expressions. A?B? does not work, since this also matches the empty string. Is it possible to create this regular expression without repeating neither A nor B ? 回答1: Edit You may add a lookahead to require at least 1 char: (?=.)A?(?:B)? ^^^^ See the regex demo. If you need to support

How do you write a Wordpress function to put a Span around the first word of the Title?

烈酒焚心 提交于 2020-01-02 06:11:09
问题 I want to replace first word in title to have <span></span> inside. Example for Wordpress title <h2 class="entry-title"><a href="#">Welcome to Wordpress</a></h2> I want to be like this <h2 class="entry-title"><a href="#"><span>Welcome</span> to Wordpress</a></h2> the function function span_on_title($span) { return preg_replace('', '', $span, 1); } add_filter('the_title','span_on_title'); May i know what to put on the preg_replace 回答1: $title = '<h2 class="entry-title"><a href="#">Welcome to

preg_replace in PHP - regular expression for NOT condition

只谈情不闲聊 提交于 2020-01-02 01:48:12
问题 I am trying to write a function in PHP using preg_replace where it will replace all those characters which are NOT found in list. Normally we replace where they are found but this one is different. For example if I have the string: $mystring = "ab2c4d"; I can write the following function which will replace all numbers with *: preg_replace("/(\d+)/","*",$mystring); But I want to replace those characters which are neither number nor alphabets from a to z. They could be anything like #$*();~!{}[

Are preg_match() and preg_replace() slow?

徘徊边缘 提交于 2020-01-02 01:44:08
问题 I've been coding in PHP for a while and I keep reading that you should only use preg_match and preg_replace when you have to because it slows down performance. Why is this? Would it really be bad to use 20 preg_matches in one file instead of using another PHP function. 回答1: As Mike Brant said in his answer: There's nothing wrong with using any of the preg_* functions, if you need them. You want to know if it's a good idea to have something like 20 preg_match calls in a single file, well,

How to remove empty html tags (wich containing whitespaces and/or their html codes)

送分小仙女□ 提交于 2020-01-01 12:35:12
问题 Need a regex for preg_replace. This question wasn't answered in "another question" because not all tags I want to remove aren't empty. I have not only to remove empty tags from an HTML structure, but also tags containing line breaks as well as white spaces and/or their html code. Possible Codes are: <br />               BEFORE removing matching tags: <div> <h1>This is a html structure.</h1> <p>This is not empty.</p> <p></p> <p><br /></p> <p> <br /> &;thinsp;</p> <p> </p> <p>   </p> </div>

Minify CSS using preg_replace

此生再无相见时 提交于 2020-01-01 10:48:36
问题 I'm trying to minify multiple CSS files using preg_replace. Actually, I'm only trying to remove any linebreaks/tabs and comments from the file. the following works for me: $regex = array('{\t|\r|\n}', '{(/\*(.*?)\*/)}'); echo preg_replace($regex, '', file_get_contents($file)); But I'd like to do it in a single multiline regex, like this: $regex = <<<EOF {( \t | \r | \n | /\*(.*?)\*/ )}x EOF; echo preg_replace($regex, '', file_get_contents($file)); However, this does not do anything at all. Is

How do I remove everything after a space in PHP?

最后都变了- 提交于 2020-01-01 07:58:23
问题 I have a database that has names and I want to use PHP replace after the space on names, data example: $x="Laura Smith"; $y="John. Smith" $z="John Doe"; I want it to return Laura John. John 回答1: Do this, this replaces anything after the space character. Can be used for dashes too: $str=substr($str, 0, strrpos($str, ' ')); 回答2: Just to add it into the mix, I recently learnt this technique: list($s) = explode(' ',$s); I just did a quick benchmark though, because I've not come across the strtok

preg_replace only OUTSIDE tags ? (… we're not talking full 'html parsing', just a bit of markdown)

社会主义新天地 提交于 2020-01-01 06:46:10
问题 What is the easiest way of applying highlighting of some text excluding text within OCCASIONAL tags "<...>"? CLARIFICATION : I want the existing tags PRESERVED! $t = preg_replace( "/(markdown)/", "<strong>$1</strong>", "This is essentially plain text apart from a few html tags generated with some simplified markdown rules: <a href=markdown.html>[see here]</a>"); Which should display as: "This is essentially plain text apart from a few html tags generated with some simplified markdown rules:

Finetune Regex to skip tags

左心房为你撑大大i 提交于 2019-12-31 07:38:25
问题 Code must skip existing links, <img> 's src values (or something like that) public function convertUrlsToLinks($text){ return preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.-]*(\?\S+)?)?)?)@', '<a href="$1" target="_blank">$1</a>', $text); } Can't figure out, what I need to modify in this function? 回答1: Add a small part to your regex that checks if your regex isn't 'inside' a tag. So it should be <tag>HERE</tag> and not <tag src="HERE"></tag>. Since a url will always be INSIDE the html

PHP - BBCode parser - recursive [quote] with regex and preg_replace

微笑、不失礼 提交于 2019-12-31 05:31:08
问题 i'm making my own bbcode parser, and i've a problem when i try to do the recursive quote. this is my code : function forumBBCode($str){ $format_search=array( '#\[quote=(.*?)\](.*?)\[/quote\]#is' ); $format_replace=array( '<blockquote class="quotearea"><i><a class="lblackbu" href="./index.php?status=userview&userv=$1">$1</a> wrote :</i><br />$2</blockquote>' ); $str=preg_replace($format_search, $format_replace, $str); $str=nl2br($str); return $str; } what i must add/edit to do a recursive