如何在<a> tags

时间:2016-10-10 18:13:16

标签: php regex

$update_text= preg_replace("/(<a[^>]*>)\s*(.*?)\s*(<\/a>)/s", "$1\n\t$2\n$3", $text);

Have this :

<a href="#" class="example">
<img src="bg1.jpg">
text text
</a>
<a href="#" class="example">
text text
<img src="bg2.jpg">
text text
</a>
<a href="#" class="example">
<img src="bg3.jpg">
</a>

Want this :

<a href="#" class="example">
  <img src="bg1.jpg">
  text text
</a>
<a href="#" class="example">
  text text
  <img src="bg2.jpg">
  text text
</a>
<a href="#" class="example">
  <img src="bg3.jpg">
</a>

https://regex101.com/r/qo2N22/3

之间的所有行添加标签

我想添加\t所有$2行,但仅应用于第一行。谁能帮助我?感谢。

1 个答案:

答案 0 :(得分:-1)

我用preg_replace_callback做到了。谢谢@ chris85的建议。

package sample;

import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class Controller {
    public TextField theight;
    public TextField tweight;


    public Label lbmi;
    public Label lresult;


    public void buttonclicked() {

        double height;
        double weight;
        double bmi;

        String result;


        height = Double.parseDouble(theight.getText());
        weight = Double.parseDouble(tweight.getText());

        double square = height * height;
        bmi = weight / square;
        lbmi.setText("The BMI is " + bmi);
    }
}