At the client’s side, sometimes (i.e., not always) data comes in a wrapper (tag) pre , is it possible to somehow remove this tag (on the server side!), But leave the content as it is?

  • one
    You can still $ nopre = str_replace (array ("<pre>", "</ pre>"), array ("", ""), $ pre); But then you can beat the other trans as data .... In general, this is a clear distortion, you need to look for another architectural solution for connecting the client-server - Chad
  • By the way, I don’t know why: Fatal error: Call to undefined function length() - Smash
  • need strlen - Chad
  • Generally read docks on php? - Chad
  • Well, it's a perversion or not, but sometimes the user can manually enter data into the editor and save and paste, so it turns out with copy-paste another unnecessary pre tag appears, and then <pre> <pre> data </ ​​pre> is displayed </ pre> (not very cool). - Smash

4 answers 4

Like this:

  $pre = $_REQUEST["CLIENT_DATA"]; $nopre = substr($pre,strlen("<pre>")); $nopre = substr($nopre,0, strlen($nopre)-strlen("</pre>")); 

:-)

  • and this will work if we assume there was no pre, i.e. Anyway content need to get the same? Keyword sometimes - Smash
  • Well then add if(substr($pre,0, strlen("<pre>"))=="<pre>") - Chad

Well, if the <pre> comes not html\xml , then there is a great function strip_tags .

 $content = strip_tags($content, '<pre>'); // удалить только <pre> 
  • Alas, it comes exactly html \ xml. I see a way out only in the editing on the client, but how to determine that two pre in a row are now this is a mystery. - Smash
  • $ ($ ("user data input"). val ()). children ("pre"). size ()> 0? - Chad
  • The answer is wrong. Your example will remove all tags EXCEPT the <pre> . - Visman
 $content = preg_replace('/^(<pre[^>]+>)+/', '', $content); $content = preg_replace('/(<\/pre>)+$/', '', $content); 

If the client copy-paste is very, very crooked.

    If you are using jQuery, then I would suggest this option - replacing the pre tag (methods .replaceWith () .replaceAll () ) with something more suitable for you.