douhuan6065 2010-05-01 15:24
浏览 37
已采纳

帮助PHP simplehtmldom - 修改表单

Ive gotten some great help here and I am so close to solving my problem that I can taste it. But I seem to be stuck.

I need to scrape a simple form from a local webserver and only return the lines that match a users local email (i.e. onemyndseye@localhost). simplehtmldom makes easy work of extracting the correct form element:

foreach($html->find('form[action*="delete"]') as $form) echo $form;

Returns:

<form action="/delete" method="post">
    <input type="checkbox" id="D1" name="D1" /><a href="http://www.linux.com/rss/feeds.php">
        http://www.linux.com/rss/feeds.php
    </a> [email: 
        onemyndseye@localhost (Default)
    ]<br />         
    <input type="checkbox" id="D2" name="D2" /><a href="http://www.ubuntu.com/rss.xml">
        http://www.ubuntu.com/rss.xml
    </a> [email: 
        onemyndseye@localhost (Default)
    ]<br />         
<input type="submit" name="delete_submit" value="Delete Selected" /></form>

However I am having trouble making the next step. Which is returning lines that contain 'onemyndseye@localhost' and removing it so that only the following is returned:

<input type="checkbox" id="D1" name="D1" /><a href="http://www.linux.com/rss/feeds.php">http://www.linux.com/rss/feeds.php</a> <br />
<input type="checkbox" id="D2" name="D2" /><a href="http://www.ubuntu.com/rss.xml">http://www.ubuntu.com/rss.xml</a> <br />

Thanks to the wonderful users of this site Ive gotten this far and can even return just the links but I am having trouble getting the rest... Its important that the complete <input> tags are returned EXACTLY as shown above as the id and name values will need to be passed back to the original form in post data later on.

Thanks in advance!

***** EDIT ******

Issue close to solved now thanks to Yacoby. The last small hurdle is that some trash is left behind from the str_ireplace. Perhaps it would be easier to remove all text between </a> and <br /> ...?

After Yacoby's additions the output is as follows:

<form action="/delete" method="post">
    <input type="checkbox" id="D1" name="D1" /><a href="http://www.linux.com/rss/feeds.php">
        http://www.linux.com/rss/feeds.php
    </a> [email: 
         (Default)
    ]<br />         
    <input type="checkbox" id="D2" name="D2" /><a href="http://www.ubuntu.com/rss.xml">
        http://www.ubuntu.com/rss.xml
    </a> [email: 
         (Default)
    ]<br />         
    <input type="checkbox" id="D3" name="D3" /><a href="http://mythbuntu.org/rss.xml">
        http://mythbuntu.org/rss.xml
    </a> [email: 

    ]<br />         
<input type="submit" name="delete_submit" value="Delete Selected" /></form>

Notice [email: (Default)] and [email: ] have been left behind. Also would need to remove the form action and submit lines at last but that part I think i can gather from the previous suggestion.

***** SOLVED ****

issue solved with:

$html = file_get_html('http://localhost:9000/');
foreach($html->find('form[action*="delete"]') as $form)
  if ( stripos($form->innertext, 'onemyndseye@localhost') !== false ){
      $form = preg_replace('!</a>.*?<br />!s', '</a><br />', $form);
      echo $form;
}

Thanks for the help!

  • 写回答

1条回答 默认 最新

  • duanlu9970 2010-05-01 15:48
    关注

    Maybe something like

    if ( stripos($form->innertext, 'onemyndseye@localhost') !== false ){
        $form->innertext = str_ireplace('onemyndseye@localhost', '', $form->innertext);
        echo $form;
    }
    

    This won't work with html like

    <b>onemyndseye</b>@localhost
    

    As it is easy to find if the text with tags removed matches a string using plaintext but it is far harder to replace.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。