如何用换行符替换XML格式的所有HTML换行符(<br/>)?

时间:2019-05-07 09:58:26

标签: php html api

我想在在线购物API中上传商品说明。因此,我想用xml中的换行符替换<br>的所有实例。但是我尝试使用下面的编码是行不通的。只是这种编码有问题,其他编码没有问题。

$product_short_description = str_replace("#<br\s*/?>#i", "\n", $product_short_description);

html details 元素下面的要求可以支持html格式,代码如下所示:

<htmlDetail>$product_short_description</htmlDetail>

下面是我的全部编码:

 <?php


include '../db_config/db_conn.php';

$sql = 'SELECT c1.entity_id AS product_id, c1.sku, c2.value AS prdNm, 
c3.value AS product_price, 
c5.value AS category_name, c6.value AS weight, c7.value AS image, c8.value 
AS description, 
c9.value AS price,c11.value AS shipment_type,c12.value AS 
warranty,c13.value AS short_description
FROM catalog_product_entity c1
LEFT JOIN catalog_product_entity_varchar c2 ON c1.entity_id=c2.entity_id 
AND c2.attribute_id=71
LEFT JOIN catalog_product_entity_decimal c3 ON c1.entity_id=c3.entity_id 
AND c3.attribute_id=75
LEFT JOIN catalog_category_product c4 ON c1.entity_id=c4.product_id
LEFT JOIN catalog_category_entity_varchar c5 ON 
c5.entity_id=c4.category_id AND c5.attribute_id=41
LEFT JOIN catalog_product_entity_decimal c6 ON c1.entity_id=c6.entity_id 
AND c6.attribute_id=80
LEFT JOIN catalog_product_entity_varchar c7 ON c1.entity_id=c7.entity_id 
 AND c7.attribute_id=85
LEFT JOIN catalog_product_entity_text c8 ON c1.entity_id=c8.entity_id AND 
c8.attribute_id=72
LEFT JOIN catalog_product_entity_decimal c9 ON c1.entity_id=c9.entity_id 
AND c9.attribute_id=75
LEFT JOIN catalog_product_entity_int c11 ON c1.entity_id=c11.entity_id AND 
c11.attribute_id=127
LEFT JOIN catalog_product_entity_int c12 ON c1.entity_id=c12.entity_id AND 
c12.attribute_id=154
LEFT JOIN catalog_product_entity_text c13 ON c1.entity_id=c13.entity_id 
AND c13.attribute_id=73
WHERE c1.entity_id = 200

';
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
$product_id = $row['product_id'];
$product_sku = $row['sku'];
$product_name = $row['prdNm'];
$product_price = $row['product_price'];
$product_category_name = $row['category_name'];
$product_weight = $row['weight'];
$product_image = $row['image'];
$product_description = $row['description'];
$product_price = $row['price'];
$product_shipment_type = $row['shipment_type'];
$product_warranty = $row['warranty'];
$product_short_description = $row['short_description'];

}

/I'm stuck in here/
$product_short_description = str_replace("#<br\s*/?>#i", "\n", $product_short_description);


$curl = curl_init();


curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.11street.my/rest/prodservices/product",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "<?xml version=\"1.0\" encoding=\"UTF-8\" 
standalone=\"yes\"?>
<Product>
<selMthdCd>01</selMthdCd>
<dispCtgrNo>4932</dispCtgrNo>
<prdTypCd>01</prdTypCd>
 <prdNm>$product_name</prdNm>
 <prdStatCd>01</prdStatCd>
 <prdWght>$product_weight</prdWght>
 <minorSelCnYn>Y</minorSelCnYn>
 <prdImage01>$product_image</prdImage01>
 <htmlDetail>$product_short_description</htmlDetail>

 </Product>",
 CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/xml",
"openapikey: asdfsdafq2e212ewsadfd",
 ),
 ));

 $response = curl_exec($curl);
 $err = curl_error($curl);

 curl_close($curl);

 echo $response;

它向我显示错误:

  

产品注册失败,需要详细说明。500

如果编码成功,将显示以下信息:

  

该产品已成功注册。产品编号:68931722200

0 个答案:

没有答案
相关问题