Escape backslash and special char in PHP – command line

Posted on Tags ,

If you ever need to escape special char with a backslash and then to escape the backslash in shell_exec() or exec() PHP functions (not only).

You will think that \\\% will do the work, backslash to escape the percentage and double backslashes to escape the backslash but this will not work.
The correct way is to again double backslash the double backslashes that escape the backslash which escapes the percentage (LooooL) 🙂 so it will look like that \\\\\%

Something like this in PHP:


$percentage = $percentageNum . '\\\\\%';
$drawText = ",drawtext=text='" . $percentage . "':fontcolor=0xffdd00:fontsize=27:font=telegrama_raw:fontfile=" . realpath($this->config['videos']['font_path']) . ":x=70:y=288:enable='between(t,0,3)'";
$cmd = $ffmpeg['bin'] . ' -y -i ' . $videoBlack . ' -min_delay 0 -ignore_loop 0 -i ' . $animationGif . ' -filter_complex "[0][1]overlay=0:0,scale=480x480,setsar=1:1' . $drawText . '" ' . $animationVideo;
shell_exec($cmd);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.