-1

i have variable which contains this html string. i want change current tag ending > to required>. its really hard for me understand preg_replace so please write me how i must build correct regex expression.

$htmlVariable =' <div class="outer required">
                <div class="form-group af-inner">
                    <label class="sr-only" for="name">~name~</label>
                    <input type="text" name="name" id="name" placeholder="~Name~" value="" size="30" data-toggle="tooltip" title="" class="form-control placeholder" >
                </div>
            </div>

            <div class="outer required">
                <div class="form-group af-inner">
                    <label class="sr-only" for="email">~mail~</label>
                    <input type="text" name="email" id="email" placeholder="~Email~" value="" size="30" data-toggle="tooltip" title="" class="form-control placeholder" >
                </div>
            </div>';

   $out = preg_replace("<input name=email  (>)","required>",$htmlVariable);

I trying change input ending > to required> if this input contains name with value email.** this is important for me. thanks

chris85
  • 23,846
  • 7
  • 34
  • 51
Dest
  • 678
  • 2
  • 9
  • 27
  • 1
    I'm not trying take free coding service from you... just want understand how this kind of expression works... – Dest Dec 09 '16 at 19:48
  • 1
    http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags Use DomDocument or an XML parser instead. – bassxzero Dec 09 '16 at 19:55
  • 1
    The expression would fail because regexs in PHP require delimiters. Your regex also would fail because `name=email` is not in your `HTML` an there are spaces and other attributes. Your current regex: https://regex101.com/r/NEQSTQ/1. Your PHP usage: https://3v4l.org/ErnrG, and the manual http://php.net/manual/en/regexp.reference.delimiters.php. – chris85 Dec 09 '16 at 19:55
  • 1
    Possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – chris85 Dec 09 '16 at 19:58

1 Answers1

2

So a few things:

  1. preg_replace requires you to use the same character to start and end your regular expression. So "input name=email " will not work - you'll want something like "/input name=email /"
  2. Your regular expression is wrong, because it fails to account for the variability in html tags... For instance, your current regex assumes "name=email" will be right after the "input" tag - but that's not the case even in your own test data. "input type="text" name="email""
  3. You also use the actual space character in your regular expression - it's better to use the whitespace regex placeholder \s

So you'll need to build a better regular expression that accounts for some variability, using regex placeholder characters:

$out = preg_replace("/(<input.*?name\s*?=[\s*?\'\"]email[\'\"].*?)>/","$1 required>",$htmlVariable);

Hope this helps!

nibnut
  • 3,072
  • 2
  • 15
  • 17
  • thanks it works perfectly and now i think i am understanding how this kind of expression works, but i have questions, what means .*? and \s*? and what is different between them? – Dest Dec 09 '16 at 23:00