【问题标题】:Replacing text inside html tags using php curl使用php curl替换html标签内的文本
【发布时间】:2014-08-13 09:07:29
【问题描述】:

我需要用元音 i 替换某些先前选择的链接上出现的所有元音 (a,e,i,o,u)。这是某种内部笑话(网站翻译应用程序)。到目前为止,我想出了这个:

    <?php
    if ($_GET['sajt']!=NULL)
    {
    $url = str_replace('&', '&amp;', $_GET['sajt']);
    $ch = curl_init(); 
    // Set url and other options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    // Get the page contents
    $html = curl_exec($ch); 

    // close curl resource to free up system resources 
    curl_close($ch);  
    $vowels = array("a", "e", "i", "o", "u");
    $onlyconsonants = str_replace($vowels, "i", $html);
    $vowels = array("A", "E", "I", "O", "U");
    $onlyconsonants = str_replace($vowels, "I", $onlyconsonants);
    echo $onlyconsonants;
    }
    ?>

但是,在我这样做之后,str_replace 也会更改 html 标签,因为整个 html 页面都存储在同一个字符串中(例如 head to hiid)。我进行了搜索,但我无法找到正确执行此操作的方法,因为它需要某种 html 解析然后将它们合并在一起

【问题讨论】:

    标签: php html curl replace tags


    【解决方案1】:

    肮脏的方式......

        $strlen = strlen( $html);
        $id = "";
        for( $i = 0; $i <= $strlen; $i++ ) {
        if ($html[$i]=='<' || $html[$i]=='>') $id = $html[$i];
        if (($html[$i]=='a' || $html[$i]=='e' || $html[$i]=='o' || $html[$i]=='u') && ($id=='>')) $html[$i] = 'i';
        if (($html[$i]=='A' || $html[$i]=='E' || $html[$i]=='O' || $html[$i]=='U') && ($id=='>')) $html[$i] = 'I';
        }
    

    【讨论】:

      猜你喜欢
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 2011-11-03
      • 2022-12-08
      相关资源
      最近更新 更多