2013-11-22 1 views
0

PHP 문자열에서 앰퍼샌드 값을 구문 분석하려고합니다. 내 코드를 실행 한 후에도 빈 값을 반환하고 내 변수 ($ area)의 '앰퍼샌드'값 때문이라고 확신합니다. 나는 htmlspecialchars, html_entity_decode를 시도했지만 아무 소용이 없습니다. 코드 아래를 참조하십시오 : 어떻게PHP 문자열에서 앰퍼샌드를 구문 분석 할 수 없습니다.

<?php 

/** Create HTTP POST */ 
$accomm = 'ACCOMM'; 
$state = ''; 
$city = 'Ballan'; 
$area = 'Daylesford & Macedon Ranges'; 
$page = '10'; 

$seek = '<parameters> 

<row><param>SUBURB_OR_CITY</param><value>'. $city .'</value></row> 
<row><param>AREA</param><value>'. $area .'</value></row> 

</parameters>'; 

$postdata = http_build_query(
array(
'DistributorKey' => '******', 
'CommandName' => 'QueryProducts', 
'CommandParameters' => $seek) 
); 

$opts = array(
'http' => array(
'method' => 'POST', 
'header' => 'Content-type: application/x-www-form-urlencoded', 
'content' => $postdata) 
); 

/** Get string output of XML (In URL instance) */ 

$context = stream_context_create($opts); 
$result = file_get_contents('http://national.atdw.com.au/soap/AustralianTourismWebService.asmx/CommandHandler?', false, $context); 

?> 

Pls는 나는

+0

try urlencode ($ area) – andreimarinescu

+0

무엇이 빈 값을 반환합니까? '$ area'의 값으로 인해 문제가 발생하는 이유는 무엇입니까? 엔티티를'$ seek' 변수에 넣기 전에 인코딩하지 않는 이유는 (CDATA 블록 내부에 있지 않는 한 엔코딩되지 않은'&'는 일반적으로 XML에서는 유효하지 않습니다)? –

+0

@andreimarinescu : 작동하지 않습니다 – akinboj

답변

2

XML은 HTML되지 않습니다 감사를 해결하고, 그 반대의 경우도 마찬가지입니다. XML 문서의 특수 문자이기 때문에 XML 문서에 베어 &을 가질 수 없습니다. 이와 같이 정적 문자열을 정의하는 경우 &amp;으로 바꿀 수 있으며 하루를 진행할 수 있습니다.

function xmlentity_encode($input) { 
    $match = array('/&/', '/</', '/>/', '/\'/', '/"/'); 
    $replace = array('&amp;', '&gt;', '&lt;', '&apos;', '&quot;'); 
    return preg_replace($match, $replace, $input); 
} 

function xmlentity_decode($input) { 
    $match = array('/&amp;/', '/&gt;/', '/&lt;/', '/&apos;/', '/&quot;/'); 
    $replace = array('&', '<', '>', '\'', '"'); 
    return preg_replace($match, $replace, $input); 
} 

echo xmlentity_encode("This is testing & 'stuff\" n <junk>.") . "\n"; 
echo xmlentity_decode("This is testing &amp; &apos;stuff&quot; n &gt;junk&lt;."); 

출력 :

This is testing &amp; &apos;stuff&quot; n &gt;junk&lt;. 
This is testing & 'stuff" n <junk>. 

나 '

당신이 또는 & 또는 다른 XML 특수 문자를 포함하거나 포함하지 않을 수있다 임의의 문자열을 인코딩해야하는 경우, 다음과 같은 기능이 필요합니다 상당히 PHP의 XML 라이브러리가 당신을 위해 을 투명하게 만들 것이라고 확신합니다., [또한 문자 집합을 존중합니다]하지만 직접 XML 문서를 작성하는 경우에는 다음과 같은 사항을 알고 있어야합니다. 이.

+0

그래, 정적 값이기 때문에 나는 &으로 바꾸려고 시도했지만 놀랍게도 어떤 값도 반환하지 않는다. 그것이 정말로 나를 당황하게하는 것입니다. 코드에 따라 이해할 수 없습니다. – akinboj

관련 문제