2013-03-21 28 views
-2

得到一些值我有一個這樣的字符串:使用PHP來找到字符串中的字符串,並從中

$string = 'this is some random text 
    <div class="myClass" title="myTitle">this is some random text 
    again</div> and some more randome text'; 

現在我想從它刪除該內容:

<div class="myClass" title="myTitle">this is some random text again</div> 

但在同一時間,我想存儲類的價值和標題的價值

任何想法如何去做到這一點?

+0

你有沒有類myClass的div? – 2013-03-21 14:13:54

+0

是的,這就是我如何知道我將如何處理它 – matt 2013-03-21 14:15:18

+1

請參閱[如何使用PHP解析和處理HTML/XML?](http://stackoverflow.com/questions/3577641/how-to-parse-and - 方法 - HTML-XML-與-PHP) – budwiser 2013-03-21 14:16:31

回答

1

下面是你的例子表示具體示例腳本:

<?php 
    $string = 'this is some random text <div class="myClass" title="myTitle"> 
      this is some random text again</div> and some more randome text'; 
    preg_match('/class=\"(\w{1,})\"\stitle=\"(\w{1,})\"/i', $string, $matches); 
    echo "Class: {$matches[1]}\n"; 
    echo "Title: {$matches[2]}\n"; 
?> 

輸出:

marks-mac-pro:~ mstanislav$ php regex.php 
Class: myClass 
Title: myTitle 

您可能需要調整用於其他用途和/或確保與命名HTML屬性公約的兼容性。