如何使用simple_html_dom替换元素

时间:2014-04-01 20:54:27

标签: php

我正在使用simple_html_dom来解析元素..

这是输出:

<span id="time1">4:00 PM </span> - <span id="title1">The Real Housewives of New York City"If You Can Make It Here"</span><br><br><span id="time2">5:00 PM </span> - <span id="title2">The Real Housewives of New York City: Give Up the Ghostwriter:    </span><br><br>

这是当前的PHP:

<?php
$errmsg_arr = array();
$errflag = false;
$link;
include ('simple_html_dom.php');

function db_connect()
{
  define('DB_HOST', 'localhost');
  define('DB_USER', 'myusername');
  define('DB_PASSWORD', 'mypassword');
  define('DB_DATABASE', 'mydbname');

  $errmsg_arr = array();
  $errflag = false;
  $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

  if(!$link) 
  {
    die('Failed to connect to server: ' . mysql_error());
  }

  $db = mysql_select_db(DB_DATABASE);
  if(!$db) 
  {
    die("Unable to select database");
  }
}

    $links = $row['links'];
    $html = file_get_html($links);
    //echo $row['links'];

    $base = $row['links'];

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_URL, $base);
    curl_setopt($curl, CURLOPT_REFERER, $base);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $str = curl_exec($curl);
    curl_close($curl);

    $html = new simple_html_dom();
    // Load HTML from a string
    $html->load($str);

    //get all category links
    /*foreach($html_base->find('a') as $element) {
        echo "<pre>";
        print_r( $element->href );
        echo "</pre>";
    }*/

    //$html_base->clear();
    //unset($html_base);

    $time1 = $html->find('span[id=row1Time]', 0)->plaintext;
    $title1 = $html->find('li[id=row1-1]', 0)->plaintext; // with this
    $output1 = preg_replace('/\d:\d+/', '', $title1);

    $html->clear();
    unset($html);


    echo '<span id="time1">'.$time1.'</span> - ';

    if (strstr($output1, '"'))
    {
      $output1 = str_replace('"', ':', $output1);
    }
?>

我试过这个:

$output1 = str_replace('"', ':', $output1);

它将用“:”替换双引号。我只想替换两个文本之间的单引号。

示例:

The Real Housewives of New York City"If You Can Make It Here"

正如您所看到的,我想要替换文本City和If之间的单引号。我想让它看起来像这样:

The Real Housewives of New York City: If You Can Make It Here

当我尝试这个时:

$output1 = str_replace('"', ':', $output1);

我会得到这样的东西:

The Real Housewives of New York City: If You Can Make It Here:

如果他们在文本中间有单引号,即使我不想替换元素末尾的单引号,你能告诉我如何查看元素吗?

我如何替换元素末尾的文本以用空文本替换单引号?

编辑:这是输出:

                         PM                                                                                                                                                                                                       The Real Housewives of Beverly Hills                                                                                               : Reunion Part 3"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                TV-14                                                                                                                                                                                                                                                                                                                                                                                                                                            &nbsp;(D,L)                                                                                                                                                                                                        <span id="time1">7:00 PM </span> - 

0 个答案:

没有答案