简体   繁体   English

从响应中的 php 数组解析 html 标签

[英]Parsing html tags from php array in the response

Having the next array:有下一个数组:

$link = 'Link ' . '<a href="https://google.com">Learn more...</a>';
$array = ['1' => 'normal string text', '2' => $link];
return $array;

The response would be:响应将是:

1 "normal string text" 1 “普通字符串文本”

2 "Link, <a href="https://google.com">Learn more..." 2 “链接,<a href="https://google.com">了解更多..."

The result expected would be that the a tag would get parsed: Link, Learn more...预期的结果是 a 标签将被解析:Link, Learn more...

Tried with different solutions, but nothing seems to be working.尝试了不同的解决方案,但似乎没有任何效果。 Any ideas?有任何想法吗?

Thanks谢谢

Edited: The result should be an array in which one of the entries would be that link already parsed and ready to be used.已编辑:结果应该是一个数组,其中一个条目是已经解析并准备好使用的链接。

well you'r code is correct but you are returning the array outside a function you need to add it to a function and call it好吧,你的代码是正确的,但你要返回 function 之外的数组,你需要将它添加到 function 并调用它

ex:前任:

  public function printArray(){
  $link = 'Link ' . '<a href="https://google.com">Learn more...</a>';
  $array = ['1' => 'normal string text', '2' => $link];
  return $array;
 }
 
//callback
 
echo printArray();

or if you want to pass some params或者如果你想传递一些参数

public function printArray($link , $text){
  $link = $link;
  $array = ['1' => $text, '2' => $link];
  return $array;
 }

    //callback 
  $link = 'Link ' . '<a href="https://google.com">Learn more...</a>';
  $text='normal string text';
 
echo printArray($link,$text);
 

or just print it like this:或者像这样打印它:

$link = 'Link ' . '<a href="https://google.com">Learn more...</a>';
$array = ['1' => 'normal string text', '2' => $link];

print($array[0]);//output : normal string text 
print($array[1]);//output :  Link Learn more...

you can use:您可以使用:

$link = 'Link ' . '<a href="https://google.com">Learn more...</a>';
$elements = ['1' => 'normal string text', '2' => $link];


foreach($elements as $key => $element){
    echo "{$key} {$element} <br>";
}

I don't know if this will work since I haven't tested it but you could try:我不知道这是否可行,因为我还没有测试过,但你可以试试:

<?php
$array->data[] = array(
  'Your text', 
  '<a href="https://google.com"><u>Learn more...</u></a>'
);
echo $array
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM