Javascript код внутри HTML код внутри строки PHP

Попытка написать строку на PHP. Строка содержит HTML-код. HTML-код содержит код Javascript внутри него. И все это в строке PHP.

текущий код:

$record["DESCRIPTION"] = preg_replace("/(<iframe.*</iframe>)/U",
'

Trying to write a string in PHP. The string contains HTML code. The HTML code contains Javascript code inside it. And all that in a PHP string.

Current Code:

$record["DESCRIPTION"] = preg_replace("/(<iframe.*</iframe>)/U",
'$1 <br> 
<button type="button" onclick="download()">تحميل</button><script>function download() {
APP.CallSub("download", true, YouTubeGetID($1));}
 function YouTubeGetID(url){ 
 var ID = "";  url = url.replace(/(>|<)/gi,"").split(/(vi/|v=|/v/|youtu.be/|/embed/)/);  if(url[2] !== undefined) {    ID = url[2].split(/[^0-9a-z_-]/i);    ID = ID[0];  }  else {    ID = url;  }    return ID;}
</script>', $record["DESCRIPTION"]);

The problem is in calling the function YouTubeGetID(). I want to call this function with the PHP regex result "$1" as a parameter. I tried:
* YouTubeGetID($1)
* YouTubeGetID("$1")

Both did not work. Do you have any ideas how to do that?

PS: the YouTubeGetID() function is working properly. I tested it with a normal string YouTubeGetID("https://youtube.com/watch?v=tN_dtNinTjk"). The problem is using the regex result $1 instead of any string.

<br>
<button type="button" onclick="download()">تحميل</button><script>function download() {
APP.CallSub("download", true, YouTubeGetID(

Trying to write a string in PHP. The string contains HTML code. The HTML code contains Javascript code inside it. And all that in a PHP string.

Current Code:

$record["DESCRIPTION"] = preg_replace("/(<iframe.*</iframe>)/U",
'$1 <br> 
<button type="button" onclick="download()">تحميل</button><script>function download() {
APP.CallSub("download", true, YouTubeGetID($1));}
 function YouTubeGetID(url){ 
 var ID = "";  url = url.replace(/(>|<)/gi,"").split(/(vi/|v=|/v/|youtu.be/|/embed/)/);  if(url[2] !== undefined) {    ID = url[2].split(/[^0-9a-z_-]/i);    ID = ID[0];  }  else {    ID = url;  }    return ID;}
</script>', $record["DESCRIPTION"]);

The problem is in calling the function YouTubeGetID(). I want to call this function with the PHP regex result "$1" as a parameter. I tried:
* YouTubeGetID($1)
* YouTubeGetID("$1")

Both did not work. Do you have any ideas how to do that?

PS: the YouTubeGetID() function is working properly. I tested it with a normal string YouTubeGetID("https://youtube.com/watch?v=tN_dtNinTjk"). The problem is using the regex result $1 instead of any string.

));}
function YouTubeGetID(url){
var ID = ""; url = url.replace(/(>|<)/gi,"").split(/(vi/|v=|/v/|youtu.be/|/embed/)/); if(url[2] !== undefined) { ID = url[2].split(/[^0-9a-z_-]/i); ID = ID[0]; } else { ID = url; } return ID;}
</script>', $record["DESCRIPTION"]);

Проблема заключается в вызове функции YouTubeGetID(). Я хочу вызвать эту функцию с результатом PHP regex "

Trying to write a string in PHP. The string contains HTML code. The HTML code contains Javascript code inside it. And all that in a PHP string.

Current Code:

$record["DESCRIPTION"] = preg_replace("/(<iframe.*</iframe>)/U",
'$1 <br> 
<button type="button" onclick="download()">تحميل</button><script>function download() {
APP.CallSub("download", true, YouTubeGetID($1));}
 function YouTubeGetID(url){ 
 var ID = "";  url = url.replace(/(>|<)/gi,"").split(/(vi/|v=|/v/|youtu.be/|/embed/)/);  if(url[2] !== undefined) {    ID = url[2].split(/[^0-9a-z_-]/i);    ID = ID[0];  }  else {    ID = url;  }    return ID;}
</script>', $record["DESCRIPTION"]);

The problem is in calling the function YouTubeGetID(). I want to call this function with the PHP regex result "$1" as a parameter. I tried:
* YouTubeGetID($1)
* YouTubeGetID("$1")

Both did not work. Do you have any ideas how to do that?

PS: the YouTubeGetID() function is working properly. I tested it with a normal string YouTubeGetID("https://youtube.com/watch?v=tN_dtNinTjk"). The problem is using the regex result $1 instead of any string.

"в качестве параметра. Я пытался:
* YouTubeGetID(

Trying to write a string in PHP. The string contains HTML code. The HTML code contains Javascript code inside it. And all that in a PHP string.

Current Code:

$record["DESCRIPTION"] = preg_replace("/(<iframe.*</iframe>)/U",
'$1 <br> 
<button type="button" onclick="download()">تحميل</button><script>function download() {
APP.CallSub("download", true, YouTubeGetID($1));}
 function YouTubeGetID(url){ 
 var ID = "";  url = url.replace(/(>|<)/gi,"").split(/(vi/|v=|/v/|youtu.be/|/embed/)/);  if(url[2] !== undefined) {    ID = url[2].split(/[^0-9a-z_-]/i);    ID = ID[0];  }  else {    ID = url;  }    return ID;}
</script>', $record["DESCRIPTION"]);

The problem is in calling the function YouTubeGetID(). I want to call this function with the PHP regex result "$1" as a parameter. I tried:
* YouTubeGetID($1)
* YouTubeGetID("$1")

Both did not work. Do you have any ideas how to do that?

PS: the YouTubeGetID() function is working properly. I tested it with a normal string YouTubeGetID("https://youtube.com/watch?v=tN_dtNinTjk"). The problem is using the regex result $1 instead of any string.

)
* YouTubeGetID("

Trying to write a string in PHP. The string contains HTML code. The HTML code contains Javascript code inside it. And all that in a PHP string.

Current Code:

$record["DESCRIPTION"] = preg_replace("/(<iframe.*</iframe>)/U",
'$1 <br> 
<button type="button" onclick="download()">تحميل</button><script>function download() {
APP.CallSub("download", true, YouTubeGetID($1));}
 function YouTubeGetID(url){ 
 var ID = "";  url = url.replace(/(>|<)/gi,"").split(/(vi/|v=|/v/|youtu.be/|/embed/)/);  if(url[2] !== undefined) {    ID = url[2].split(/[^0-9a-z_-]/i);    ID = ID[0];  }  else {    ID = url;  }    return ID;}
</script>', $record["DESCRIPTION"]);

The problem is in calling the function YouTubeGetID(). I want to call this function with the PHP regex result "$1" as a parameter. I tried:
* YouTubeGetID($1)
* YouTubeGetID("$1")

Both did not work. Do you have any ideas how to do that?

PS: the YouTubeGetID() function is working properly. I tested it with a normal string YouTubeGetID("https://youtube.com/watch?v=tN_dtNinTjk"). The problem is using the regex result $1 instead of any string.

")

Оба не сработали. У вас есть идеи, как это сделать?

PS: YouTubeGetID()функция работает правильно. Я проверил его с помощью обычной строки YouTubeGetID("https://youtube.com/watch?v=tN_dtNinTjk"). Проблема заключается в использовании результата regex

Trying to write a string in PHP. The string contains HTML code. The HTML code contains Javascript code inside it. And all that in a PHP string.

Current Code:

$record["DESCRIPTION"] = preg_replace("/(<iframe.*</iframe>)/U",
'$1 <br> 
<button type="button" onclick="download()">تحميل</button><script>function download() {
APP.CallSub("download", true, YouTubeGetID($1));}
 function YouTubeGetID(url){ 
 var ID = "";  url = url.replace(/(>|<)/gi,"").split(/(vi/|v=|/v/|youtu.be/|/embed/)/);  if(url[2] !== undefined) {    ID = url[2].split(/[^0-9a-z_-]/i);    ID = ID[0];  }  else {    ID = url;  }    return ID;}
</script>', $record["DESCRIPTION"]);

The problem is in calling the function YouTubeGetID(). I want to call this function with the PHP regex result "$1" as a parameter. I tried:
* YouTubeGetID($1)
* YouTubeGetID("$1")

Both did not work. Do you have any ideas how to do that?

PS: the YouTubeGetID() function is working properly. I tested it with a normal string YouTubeGetID("https://youtube.com/watch?v=tN_dtNinTjk"). The problem is using the regex result $1 instead of any string.

вместо любой строки.

1 ответ

  1. Ваш пример работает. Вот его форматированная версия. Я запускаю его на PHP песочнице без каких-либо проблем. Единственная настройка состояла в том, чтобы изменить жадный селектор .*на не жадный .*?.

    Я предполагаю, что ваш сгенерированный код JavaScript является проблемой здесь. Созданный код JavaScript недопустим. Вам нужно избежать всего

    Your example is working. Here is a formatted version of it. I run it on a PHP Sandbox without any problem. The only adjustment was to change the greedy selector .* to a non-greedy one .*?.

    I'm guessing, your generated JavaScript code is the problem here. The generated JavaScript code is not valid. You need to escape everything in $1 and surround it with brackets.

    Here is a formatted version of your code.

    <?php
        $record = array("DESCRIPTION" => '<iframe src="http://www.google.com"></iframe>');
    
        $replacement = '$1<br>';
        $replacement .= '<button type="button" onclick="download()">تحميل</button>';
        $replacement .= '<script>';
        $replacement .= 'function download() {';
        $replacement .= '   APP.CallSub("download", true, YouTubeGetID($1));';
        $replacement .= '}';
        $replacement .= 'function YouTubeGetID(url){';
        $replacement .= '   var ID = "";';
        $replacement .= '   url = url.replace(/(>|<)/gi,"").split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);';
        $replacement .= '   if(url[2] !== undefined) {';
        $replacement .= '       ID = url[2].split(/[^0-9a-z_\-]/i);';
        $replacement .= '       ID = ID[0];';
        $replacement .= '   } else {';
        $replacement .= '       ID = url,';
        $replacement .= '   }';
        $replacement .= '   return ID;';
        $replacement .= '}';
        $replacement .= '</script>';
    
        $record["DESCRIPTION"] = preg_replace("/(<iframe.*?<\/iframe>)/U", $replacement, $record["DESCRIPTION"]);
    
        echo $record["DESCRIPTION"];
    ?>
    

    And here is the output of this code:

    <iframe src="http://www.google.com"></iframe><br><button type="button" onclick="download()">تحميل</button><script>function download() { APP.CallSub("download", true, YouTubeGetID(<iframe src="http://www.google.com"></iframe>));}function YouTubeGetID(url){ var ID = "";    url = url.replace(/(>|<)/gi,"").split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/); if(url[2] !== undefined) {      ID = url[2].split(/[^0-9a-z_\-]/i);     ID = ID[0]; } else {        ID = url,   }   return ID;}</script>
    

    If you need further help, please provide a comment below.

    и окружить его скобками.

    Вот форматированная версия вашего кода.

    <?php
        $record = array("DESCRIPTION" => '<iframe src="http://www.google.com"></iframe>');
    
        $replacement = '
    	

    Your example is working. Here is a formatted version of it. I run it on a PHP Sandbox without any problem. The only adjustment was to change the greedy selector .* to a non-greedy one .*?.

    I'm guessing, your generated JavaScript code is the problem here. The generated JavaScript code is not valid. You need to escape everything in $1 and surround it with brackets.

    Here is a formatted version of your code.

    <?php
        $record = array("DESCRIPTION" => '<iframe src="http://www.google.com"></iframe>');
    
        $replacement = '$1<br>';
        $replacement .= '<button type="button" onclick="download()">تحميل</button>';
        $replacement .= '<script>';
        $replacement .= 'function download() {';
        $replacement .= '   APP.CallSub("download", true, YouTubeGetID($1));';
        $replacement .= '}';
        $replacement .= 'function YouTubeGetID(url){';
        $replacement .= '   var ID = "";';
        $replacement .= '   url = url.replace(/(>|<)/gi,"").split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);';
        $replacement .= '   if(url[2] !== undefined) {';
        $replacement .= '       ID = url[2].split(/[^0-9a-z_\-]/i);';
        $replacement .= '       ID = ID[0];';
        $replacement .= '   } else {';
        $replacement .= '       ID = url,';
        $replacement .= '   }';
        $replacement .= '   return ID;';
        $replacement .= '}';
        $replacement .= '</script>';
    
        $record["DESCRIPTION"] = preg_replace("/(<iframe.*?<\/iframe>)/U", $replacement, $record["DESCRIPTION"]);
    
        echo $record["DESCRIPTION"];
    ?>
    

    And here is the output of this code:

    <iframe src="http://www.google.com"></iframe><br><button type="button" onclick="download()">تحميل</button><script>function download() { APP.CallSub("download", true, YouTubeGetID(<iframe src="http://www.google.com"></iframe>));}function YouTubeGetID(url){ var ID = "";    url = url.replace(/(>|<)/gi,"").split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/); if(url[2] !== undefined) {      ID = url[2].split(/[^0-9a-z_\-]/i);     ID = ID[0]; } else {        ID = url,   }   return ID;}</script>
    

    If you need further help, please provide a comment below.

    <br>';
    $replacement .= '<button type="button" onclick="download()">تحميل</button>';
    $replacement .= '<script>';
    $replacement .= 'function download() {';
    $replacement .= ' APP.CallSub("download", true, YouTubeGetID(

    Your example is working. Here is a formatted version of it. I run it on a PHP Sandbox without any problem. The only adjustment was to change the greedy selector .* to a non-greedy one .*?.

    I'm guessing, your generated JavaScript code is the problem here. The generated JavaScript code is not valid. You need to escape everything in $1 and surround it with brackets.

    Here is a formatted version of your code.

    <?php
        $record = array("DESCRIPTION" => '<iframe src="http://www.google.com"></iframe>');
    
        $replacement = '$1<br>';
        $replacement .= '<button type="button" onclick="download()">تحميل</button>';
        $replacement .= '<script>';
        $replacement .= 'function download() {';
        $replacement .= '   APP.CallSub("download", true, YouTubeGetID($1));';
        $replacement .= '}';
        $replacement .= 'function YouTubeGetID(url){';
        $replacement .= '   var ID = "";';
        $replacement .= '   url = url.replace(/(>|<)/gi,"").split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);';
        $replacement .= '   if(url[2] !== undefined) {';
        $replacement .= '       ID = url[2].split(/[^0-9a-z_\-]/i);';
        $replacement .= '       ID = ID[0];';
        $replacement .= '   } else {';
        $replacement .= '       ID = url,';
        $replacement .= '   }';
        $replacement .= '   return ID;';
        $replacement .= '}';
        $replacement .= '</script>';
    
        $record["DESCRIPTION"] = preg_replace("/(<iframe.*?<\/iframe>)/U", $replacement, $record["DESCRIPTION"]);
    
        echo $record["DESCRIPTION"];
    ?>
    

    And here is the output of this code:

    <iframe src="http://www.google.com"></iframe><br><button type="button" onclick="download()">تحميل</button><script>function download() { APP.CallSub("download", true, YouTubeGetID(<iframe src="http://www.google.com"></iframe>));}function YouTubeGetID(url){ var ID = "";    url = url.replace(/(>|<)/gi,"").split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/); if(url[2] !== undefined) {      ID = url[2].split(/[^0-9a-z_\-]/i);     ID = ID[0]; } else {        ID = url,   }   return ID;}</script>
    

    If you need further help, please provide a comment below.

    ));';
    $replacement .= '}';
    $replacement .= 'function YouTubeGetID(url){';
    $replacement .= ' var ID = "";';
    $replacement .= ' url = url.replace(/(>|<)/gi,"").split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);';
    $replacement .= ' if(url[2] !== undefined) {';
    $replacement .= ' ID = url[2].split(/[^0-9a-z_\-]/i);';
    $replacement .= ' ID = ID[0];';
    $replacement .= ' } else {';
    $replacement .= ' ID = url,';
    $replacement .= ' }';
    $replacement .= ' return ID;';
    $replacement .= '}';
    $replacement .= '</script>';

    $record["DESCRIPTION"] = preg_replace("/(<iframe.*?<\/iframe>)/U", $replacement, $record["DESCRIPTION"]);

    echo $record["DESCRIPTION"];
    ?>

    И вот вывод этого кода:

    <iframe src="http://www.google.com"></iframe><br><button type="button" onclick="download()">تحميل</button><script>function download() { APP.CallSub("download", true, YouTubeGetID(<iframe src="http://www.google.com"></iframe>));}function YouTubeGetID(url){ var ID = "";    url = url.replace(/(>|<)/gi,"").split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/); if(url[2] !== undefined) {      ID = url[2].split(/[^0-9a-z_\-]/i);     ID = ID[0]; } else {        ID = url,   }   return ID;}</script>
    

    Если вам нужна дополнительная помощь, пожалуйста, предоставьте комментарий ниже.