I want to replace braced word to input box ;
$regex = "/\{(\w+)\}/" ;
$string = " i have a {dream}. <br> you have to {dream} " ;
$output = preg_replace( $regex , '<input class="gts-input" value="$1" />' , $string );
it's good
But multi word in brace. it does not works
$regex = "/\{(\w+)\}/" ;
$string = " i have a {dream}. <br> you have to {dream dream} " ;
$output = preg_replace( $regex , '<input class="gts-input" value="$1" />' , $string );
==> it not works. did not change second brace {dream dream} How i do? help me please.
You can use
<?php
$regex = "/(?:\G(?!\A)|\{)\s*(\w+)(?=[^{}]*})}?/" ;
$string = " i have a {dream}. <br> you have to {dream dream} " ;
$output = preg_replace( $regex , '<input class="gts-input" value="$1" />' , $string );
echo $output;
See the PHP demo . Output:
i have a <input class="gts-input" value="dream" />. <br> you have to <input class="gts-input" value="dream" /><input class="gts-input" value="dream" />
Details
(?:\G(?!\A)|\{)
- either the end of the previous successful match or a {
char \s*
- zero or more whitespaces (\w+)
- Group 1: one or more word chars (?=[^{}]*})
- a positive lookahead that matches a position that is immediately followed with zero or more chars other than {
and }
and then a }
char }?
- an optional }
.
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.