convert the url & hashtag into link

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts: 190
Location: Holland Rotterdam

convert the url & hashtag into link

I'm sorry to interrupt, I've been trying to ask a question on stackoverflow for an hour but I can not figure it out because they've changed their comment system

I have a function to convert the url & hashtag into link
this is working fine.... But my problem is I have
in my database also URL-links + Iframes but those
are not completely visible how it should be

I tried things with call-back function
but I can not figure it out ????

So I can`t make my URL-links + Iframes visible

I hope you have some ideas ????

Code: Select all

      <?php
      //function to convert the url & hashtag into link
      function convert_to_links($msg){
      $final_message = preg_replace(array('/()#()/i'), array('<a href="$1" target="_blank">$1</a>', '$1<a href="">@$2</a>', '$1<a href="Read-Item.php?hashtag=$2">#$2</a>'), $msg);
      return $final_message;
      }

      //if hashtag is requested
      if(isset($_GET['hashtag']) && !empty($_GET['hashtag'])){
      $hashtag = mysql_real_escape_string(strip_tags(trim($_GET['hashtag'])));
      $query = mysql_query("SELECT * FROM politicsnucleus_item WHERE imore LIKE '%$hashtag%' ORDER BY inumber DESC");
      $title = "Search Result For <span style='color:red;'>".$hashtag."</span> <a href='hashtag.php'>clear</a>";
      }else{ // if not
      $query = mysql_query("SELECT * FROM politicsnucleus_item ORDER BY inumber DESC LIMIT 15");
      $title = "All Updates";
      }


      ?>



      <?php

      echo $title;
      //display the messages
      if(mysql_num_rows($query) > 0){
      while ($row = mysql_fetch_assoc($query)) {
      $final_msg = convert_to_links($row['imore']);


       echo '
    
       <ul>
       <li> '.$final_msg.'</li>
   
       ';
        }
        }
        ?>
        <!--post will go here-->
        </div>

Admin
I not understand because i not know what you have in database and what result you wand to display.

JanMolendijk
I had problems with Iframe`s + URL`s but after a whole day trying I finally found out what I did do wrong
`thanks for the support`

JanMolendijk
I place it in the same topic but I`m still busy with #hashtag
My problem is I would like only detect the #hashtag

Code: Select all

<?php
// connect to the "tests" database
$conn = new mysqli('127.0.0.1', 'Politics', '123456', 'Politics');

//function to convert the url & hashtag into link
 function convert_to_links($msg){
  $final_message = preg_replace(array('/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))/', '/(^|[^a-z0-9_])@([a-z0-9_]+)/i', '/(^|[^a-z0-9_])#([a-z0-9_]+)/i'), array('<a href="$1" target="_blank">$1</a>', '$1<a href="">@$2</a>', '$1<a href="http://145.53.93.209/hashtag/index.php?hashtag=$2">#$2</a>'), $msg);
  return $final_message;
 }


  if(isset($_GET['hashtag']) && !empty($_GET['hashtag'])){
   $hashtag = mysqli_real_escape_string(strip_tags(trim($_GET['hashtag'])));
   $query = mysqli_query("SELECT * FROM politicsnucleus_item WHERE imore LIKE '%$hashtag%' ORDER BY inumber DESC");

  }

/* change character set to utf8 */
if (!mysqli_set_charset($conn, "utf8")) {
    printf("", mysqli_error($link));
    exit();
} else {

}

// check connection

if (mysqli_connect_errno()) {
  exit('Connect failed: '. mysqli_connect_error());
}
// SELECT sql query
$sql = "SELECT * FROM `politicsnucleus_item`  WHERE  inumber='$id' ORDER BY `inumber` DESC LIMIT 1"; 

// perform the query and store the result
$result = $conn->query($sql);
// if the $result contains at least one row
if ($result->num_rows > 0) {
  // output data of each row from $result
  while($row = $result->fetch_assoc()) {
   $row['imore'] = convert_to_links($row['imore']);  

  echo ' 

<p>'. $row['imore']. '</p>' ;

  }
}
else {
  echo 'No-one added yet a @1';
}
$conn->close();
?>
I have in '. $row['imore']. ' an Iframe but the code does not show it

Code: Select all

<iframe src="145.53.93.209/Politics/createaccount.php" width="100%" height="100%" frameborder="0" scrolling="yes" align="top" marginwidth="0" marginheight="0" name="offtopic"></iframe>

Admin
Here is an example that gets the url address and #hashtag:

Code: Select all

$url ='some_domai.net/dir/page#hashtag';
$ar_u =[];
if(preg_match('/([^#]+)([#]{0,1}[^#]*)/i', $url, $mt)) $ar_u = $mt;
var_export($ar_u);  // array( 0 => 'some_domai.net/dir/page#hashtag', 1 => 'some_domai.net/dir/page', 2 => '#hashtag')

Similar Topics