简体   繁体   中英

Wordpress get links from field textarea

i make textarea field in wordpress

$xbox->add_field(
        array(
            'id'   => 'examplelink',
            'name' => esc_html__( 'examplelink', 'wpst' ),
            'type' => 'textarea',
            'desc' => esc_html__( 'test', 'wpst' ),
        )
    );

In textarea i put random multi link example:

https://www.google.com/random-text

https://www.yahoo.com/random-text

https://www.example.com//random-text

how i can call in outside post to get each link alone

<a href="https://www.google.com/random-text" target="_blank">google.com</a>
<a href="https://www.google.com/random-text" target="_blank">yahoo.com</a>
<a href="https://www.yahoo.com/random-text" target="_blank">yahoo.com</a>

EDIT: i tried this solution and is worked

<?php $lines = explode("\n", $examplelink);
if ( !empty($lines) ) {
  
  foreach ( $lines as $line ) {
    echo '<a class="my-button" href="'. trim( $line ) .'" target="_blank">Link Name</a>' ; 
  }

}
?>

how i can get every href name with this

<?= parse_url(($examplelink), PHP_URL_HOST); ?>

Thanks everyone, I GOT IT: this will get each link with name of the domain The final code:

<?php $lines = explode("\n", $examplelink); 
if ( !empty($lines) ) {
  
  foreach ( $lines as $line ) {
    echo '<a href="'. trim( $line ) .'" target="_blank">'. parse_url(($line), PHP_URL_HOST) .'</a>' ; 
}}?>

You can do it like below.

<?php
function linkExtractor($html)
{
 $linkArray = array();
 if(preg_match_all('/<a\s+.*?href=[\"\']?([^\"\' >]*)[\"\']?[^>]*>(.*?)<\/a>/i', $html, $matches, PREG_SET_ORDER)){
  foreach ($matches as $match) {
   array_push($linkArray, array($match[1], $match[2]));
  }
 }
 return $linkArray;
}
$links = linkExtractor($examplelink);
// Your code...
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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