php preg_replace一个标签之间的文本

时间:2022-05-02 22:26:27

I have a website which create many forms as following:

我有一个网站,可以创建以下多种形式:

<form action="link1" method="post" >
<form action="link2" method="post" >
<form action="link3" method="post" > etc ...

Now all I want is to change the whole text between the form tag so it will look like this:

现在我想要的是更改表单标记之间的整个文本,使它看起来像这样:

<form id="link">

I already tested many tricks but I couldn't solve my problem.

我已经测试了很多技巧,但我无法解决我的问题。

4 个解决方案

#1


1  

Did you try this ?

你试过这个吗?

$mixed = preg_replace('~(<form [^>]+>)(.+?)+(</form>)~i', '$1the form HTML replacement$3', '<form method="post">the form HTML content to replace</form>');

I used this link to test it http://micmap.org/php-by-example/en/function/preg_replace

我用这个链接来测试它http://micmap.org/php-by-example/en/function/preg_replace

#2


1  

It's not clear what you're exactly trying to do, but you should consider DOM` for the task.

目前还不清楚你正在尝试做什么,但你应该考虑DOM`来完成任务。

$doc = DOMDocument::loadHTML('
     <form action="link1" method="post" >
     <form action="link2" method="post" >
     <form action="link3" method="post" >
');

foreach ($doc->getElementsByTagName('form') as $node) {
   $node->removeAttribute('action');
   $node->removeAttribute('method');
   $node->setAttribute('id', 'somelink');
}

#3


0  

let's assume that your code generate an array $links[] like this:

让我们假设您的代码生成一个数组$ links [],如下所示:

$links = array("link1", "link2", "link3"); 

so your code would look something like this :

所以你的代码看起来像这样:

$re = "/\\<form action=\\\"([a-zA-Z0-9_-]+)\\\" method=\\\"post\\\" \\>/"; 

foreach($links as $value)
 {

    $str = "<form action=\"$value\" method=\"post\" >"; 
    $subst = "<form id=\"$1\"> <input type=\"text\"></form>"; 

    $new_link = preg_replace($re, $subst, $str, 1);
    echo $new_link;
    echo "<br>";
 }

click here to see it in action or here (if the first link is dead)

点击这里查看它在行动或这里(如果第一个链接已死)

and here is the $re variable in details :

这里是$ re变量的详细信息:

php preg_replace一个标签之间的文本

hope that helps!

希望有所帮助!

#4


0  

try this:

尝试这个:

preg_replace("~<form\s.*?>~i",'<form id="link">', '<form action="link3" method="post" >');

#1


1  

Did you try this ?

你试过这个吗?

$mixed = preg_replace('~(<form [^>]+>)(.+?)+(</form>)~i', '$1the form HTML replacement$3', '<form method="post">the form HTML content to replace</form>');

I used this link to test it http://micmap.org/php-by-example/en/function/preg_replace

我用这个链接来测试它http://micmap.org/php-by-example/en/function/preg_replace

#2


1  

It's not clear what you're exactly trying to do, but you should consider DOM` for the task.

目前还不清楚你正在尝试做什么,但你应该考虑DOM`来完成任务。

$doc = DOMDocument::loadHTML('
     <form action="link1" method="post" >
     <form action="link2" method="post" >
     <form action="link3" method="post" >
');

foreach ($doc->getElementsByTagName('form') as $node) {
   $node->removeAttribute('action');
   $node->removeAttribute('method');
   $node->setAttribute('id', 'somelink');
}

#3


0  

let's assume that your code generate an array $links[] like this:

让我们假设您的代码生成一个数组$ links [],如下所示:

$links = array("link1", "link2", "link3"); 

so your code would look something like this :

所以你的代码看起来像这样:

$re = "/\\<form action=\\\"([a-zA-Z0-9_-]+)\\\" method=\\\"post\\\" \\>/"; 

foreach($links as $value)
 {

    $str = "<form action=\"$value\" method=\"post\" >"; 
    $subst = "<form id=\"$1\"> <input type=\"text\"></form>"; 

    $new_link = preg_replace($re, $subst, $str, 1);
    echo $new_link;
    echo "<br>";
 }

click here to see it in action or here (if the first link is dead)

点击这里查看它在行动或这里(如果第一个链接已死)

and here is the $re variable in details :

这里是$ re变量的详细信息:

php preg_replace一个标签之间的文本

hope that helps!

希望有所帮助!

#4


0  

try this:

尝试这个:

preg_replace("~<form\s.*?>~i",'<form id="link">', '<form action="link3" method="post" >');