使用simple_html_dom替换字符串

时间:2014-04-01 17:52:38

标签: php

我正在使用PHP来解析字符串,使用simple_html_dom。

我得到的返回字符串是这样的:

<span id="title1">Reba"House Rules" (D) </span>

这是当前的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('" (D) ', '', $output1);

它不会让我替换我正在寻找的字符串。

我只能使用这样的东西:

$output1 = str_replace('(D)', '', $output1);

在替换之前我能以其他方式告诉我如何查看我正在寻找的文字的字符串吗?

0 个答案:

没有答案