2016-10-11 1 views
-1

내가이 HTML 파일 items.php이 있습니다/I가 가지고있는 항목 (들)을 찾기 위해 preg_match를 사용하고 싶습니다, displayItems.php에서이러한 HTML 태그를 preg_match하는 방법은 무엇입니까?

<all> 
    <designItems base="http://website.com/cms/items/"> 
    <item price="20" contentId="10928" name="Designed by X091"> 
     <hair set="10"> 
     <data> 
      <![CDATA[ 
      [{"c":110092,"y":10,"x":34}] 
      ]]> 
     </data> 
     </hair> 
    </item> 

    <item price="90" contentId="10228" name="Designed by XXX"> 
     <hair set="2"> 
     <data> 
      <![CDATA[ 
      [{"c":110022,"y":-2,"x":90}] 
      ]]> 
     </data> 
     </hair> 
    </item> 
    </designItems> 
</all> 

name="Designed by X091"을 얻을 그 가격 및 콘텐츠 ID, 그리고 그것의 머리를 설정하고 자료.

preg_match로 가능합니까? 감사합니다 :)

+0

물론 가능합니다. 지금까지 뭐 해봤 어? –

+0

@ Magnus Eriksson 어디서부터 시작해야할지 모르겠습니다. HTML을 검색하는'preg_match'에 대한 모든 해답은 특정한 HTML 속성을 가지고 있지만 매시간의 가격과 contentId 속성은 매번 다릅니다. –

+2

이것은 실제로 HTML과 같지 않지만 XML과 같습니다. 'preg_match()'대신에 PHP의 XML 함수를 사용하여 속성을 읽으십시오. SimpleXML에 대한 자세한 내용은 http://php.net/manual/en/simplexml.examples-basic.php를 참조하십시오. –

답변

2

이 경우 잘못된 구문 분석의 잠재력이 너무 크기 때문에 정규 표현식을 사용하고 싶지 않습니다. 대신 SimpleXML 또는 이와 유사한 구문 분석 라이브러리를 사용해야합니다.

그런 다음 간단한 루프를 사용하여 이름 속성을 확인할 수 있습니다. 이것과 같은 것, 즉 :

$items = new SimpleXMLElement ($items); 

// Loop through all of the elements, storing the ones we want in an array. 
$wanted = array(); 
foreach ($items->designItems->item as $current) { 
    // Skip the ones we're not interested in. 
    if ($current['title'] != 'designed by aaaa') { 
     continue; 
    } 

    // Do whatever you want with the item here. 
} 
관련 문제