So I have attempted to get anything with a #texthere t change to a link so i can navigate them to the hash page of that specific hash.
With the code I have I just keep getting 'undefined' back. Can someone please take a look and point out where I am going wrong.
$json = array(
'userpost' => array()
);
$row = mysqli_fetch_assoc($check1);
$posts = array();
$posts['num'] = $num;
$posts['streamitem_id'] = $row['streamitem_id'];
$autoembed = new AutoEmbed();
$posts['streamitem_content'] = $autoembed->parse($row['streamitem_content']);
$regex = "/#(\w+)/";
$string=$row['streamitem_content'];
$string = preg_replace($regex, '$1', $string);
$posts['streamitem_content']=json_decode($string);
$posts['streamitem_creator'] = $row['streamitem_creator'];
$posts['streamitem_timestamp'] = $row['streamitem_timestamp'];
$posts['username'] = $row['username'];
$posts['id'] = $row['id'];
$posts['first'] = $row['first'];
$posts['middle'] = $row['middle'];
$posts['last'] = $row['last'];
$json['userpost'][] = $posts;
echo json_encode($json);
Okay this is what I've done to fix the issue. no decoding needed and that is what was causing the issue.
$regex = "/#(\w+)/";
$posts['streamitem_content'] = $row['streamitem_content'];
$posts['streamitem_content'] = preg_replace($regex, "
<a href='hash.php?tag=$1'>$1</a>", $posts['streamitem_content'] );
Related
I have been trying to print an output using json_encode, the code is below:
<?
$lid = $_GET['last_id'];
$sql = "SELECT * FROM tbl_posts WHERE id < $lid ORDER BY id DESC LIMIT 10";
$result = mysqli_query($con,$sql);
$json = include('datam.php');
echo json_encode($json);
?>
Here is the datam.php file:
<?
$count = 0;
while($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$likes = $row['likes'];
$dislikes = $row['dislikes'];
$text = $row['text'];
$text = htmlspecialchars($text);
$title = substr($text, 0, 50);
$title = preg_replace('~[^\\pL\d]+~u', '-', $title);
$title = trim($title, '-');
$title = strtolower($title);
$title = preg_replace('~[^-\w]+~', '', $title);
if(empty($title)) {
$title = "no-title";
}
$cat = $row['cat'];
$cat = strtolower($cat);
$cat = str_replace(" ","-",$cat);
$ccat = str_replace("-"," ",$cat);
$ccat = ucwords($ccat);
$by = $row['uid'];
$dt = $row['date'];
$tm = $row['time'];
$time = strtotime("$dt $tm");
$nsfw = $row['nsfw'];
if ($nsfw == 1)
$isnsfw = '<span class="nsfw">NSFW</span>';
else
$isnsfw = "";
// Time Ago
// Get OP
$suser = "SELECT * FROM tbl_users WHERE id = $by";
$muser = mysqli_query($con,$suser);
$guser = mysqli_fetch_array($muser);
$byuser = $guser['user'];
$byuser = strtolower($byuser);
$byuser = str_replace(" ","-",$byuser);
$cbyuser = str_replace("-"," ",$byuser);
$cbyuser = ucwords($cbyuser);
// WhatsApp Link
$wm = preg_replace("/\n/", "%0A", $text);
if($count == 5 && $cnsfw == 0) {
include "adapp.php";
}
echo '<div class="box" id="'.$id.'"><div class="t">Posted by '.$cbyuser.' in '.$ccat.' '.ago($time).' ago</div><div class="m">'.$text.'</div><div class="m"><span class="col-3"><span class="likes">'.$likes.' Likes</span></span><span class="col-3"><span class="dislikes">'.$dislikes.' Dislikes</span></span><span class="col-3">'.$isnsfw.'</span></div><div class="b"><span class="col-5 l bbox"><img src="/img/like.png" /></span><span class="col-5 l bbox"><img src="/img/dislike.png" /></span><span class="col-5 l bbox"><img src="/img/comment.png" /></span><span class="col-5 l bbox"><a rel="nofollow" href="whatsapp://send?text=http://whatsappstatus.in/msg/'.$id.'%0A'.$wm.'"><img src="/img/whatsapp.png" /></a></span><span class="col-5 l bbox"><img src="/img/options.png" /></span></div><div class="clr"></div></div>';
$count++;
}
?>
Using this code, all the output appears as expected but, at the end of each output, there is unexpected output 1 which has no source of origin.
Every time this code executes, it gives an unexpected 1 at the end and I can't seem to find where it is coming from.
Here is the picture of sample output:
You can visit this URL to check it: http://funpd.com/messages1
When using
$json = include('datam.php');
In your datam.php you should return the value of the data you want assigning to $json rather than echoing it out. The echo in the datam.php is code which is displaying the data and the 1 is the value being assigned to $json (which is the value returned by your datam.php page).
You can test this by commenting out the echo in the sub page and you should just see the value 1 being displayed.
It may be the easiest solution looking at your code to just include this sub-page and not assign the value to $json, but you may have a reason for json encoding the return value.
Update:
To batch data up, build an array of data. At the top
$output = [];
After your echo - build an array of the results and add it to output array...
$output[] = [$id, $byuser, $cbyuser, ...];
(You will need to complete this with all of the fields you need)
And at the end
return $output;
This will pass all the data back to the calling page.
So far what i have been trying to do is output a video response i know it wont work if i dont have the $id after the tag but before the tag it wont even work if the str_replace doesn't work.
<?php
$html = "";
$url = "https://www.youtube.com/feeds/videos.xml?user=Fliberjig1";
$xml = simplexml_load_file($url);
for($i = 0; $i < 6; $i++) {
$id = $xml->entry[$i]->id;
$id = str_replace("YT:VIDEO:","", $id);
$title = $xml->entry[$i]->title;
$html .= "<li><h3>$title</h3>
<p>$id</p>
<iframe width='854' height='480' src='https://www.youtube.com/embed/$id' frameborder='0' allowfullscreen></iframe></li>";
}
echo $html;
?>
the result will be like this
YT:VIDEO:WOCIEMNSI4C
but i want it to be like this
WOCIEMNSI4C
Can you please help in any way to help me with my problem
If I understand you correctly, you just want to parse the string. This can be done using:
$pieces = explode(":", $id);
$clean_id = $pieces[2];
It seems that the feed contains yt:video:WOCIemNSI4c and not YT:VIDEO:WOCIEMNSI4C so what you actually need is the str_ireplace
function instead of str_replace you're currently using.
So if you change your 7th line of code to this:
$id = str_ireplace("YT:VIDEO:","", $id);
you should be ok.
$id = "YT:VIDEO:WOCIEMNSI4C";
$rest = substr("$id", -11); //result WOCIEMNSI4C
My JSON result http://daysof.me/lowyat/list.php
What I did:
$num = 0;
$array = array();
foreach($rows as $go){
if($num == count($rows)-1){break;}
$reply = $go->find('td',3)->plaintext;//replies
$starter = $go->find('td',4)->plaintext;//starter
$views = $go->find('td',5)->plaintext;//views
$action = $go->find('td',6)->plaintext;//last action
$desc = $go->find('td',2)->find('div div',2)->plaintext;//description
$title = $go->find('td',2)->find('div div a',0)->plaintext;//topic name
$link = $go->find('td',2)->find('div div a',0)->href;
$array[]= array(
'title'=>$title,
'desc'=>$desc,
'starter'=>trim($starter),
'replies'=>trim($reply),
'url'=>'https://forum.lowyat.net'.$link
);
$num ++;
}
echo json_encode($array);
You'll see some single quote been turned into " and ampersand gave strange code.
How can I escape that? I tried 'title'=>htmlentities($title) still no luck with that.
I have the code:
mysql_connect('...');
mysql_select_db('...');
$result = mysql_query('SELECT `oldword`, `newword` FROM `#_words`');
$find = array();
$replace = array();
while ($row = mysql_fetch_assoc($result)) {
$find[] = $row['oldword'];
$replace[] = $row['newword'];
}
$oldstring = 'Some text';
$newstring = str_replace($find, $replace, $oldstring);
echo $newstring;
It works good but there are two problems. First I need to convert it for using Joomla API i.e. to something like
$db = &JFactory::getDBO();
$db->setQuery('SELECT `oldword`, `newword` FROM `#_words`');
// and what is next, what about mysql_fetch_assoc ?
And secondly, if oldword and newword are english it works but if cyrillic it doesn't. How can I fix it? I have tried this:
function fixEncoding($s, $encoding = 'UTF-8') {
$s = #iconv('UTF-16', $encoding . '//IGNORE', iconv($encoding, 'UTF-16//IGNORE', $s));
return str_replace("\xEF\xBB\xBF", '', $s);
}
$find = fixEncoding($find);
$replace = fixEncoding($replace);
but this function works with a single string only and doesn't work with array
I would have to do some testing regarding the cyrillic problem, but as for your database query using Joomla coding standards, it will look like this:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('oldword', 'newword')))
->from($db->quoteName('#__words'));
$db->setQuery($query);
$results = $db->loadAssocList();
$find = array();
$replace = array();
foreach ( $results as $row ) {
$find[] = $row->oldword;
$replace[] = $row->newword;
}
$oldstring = 'Some text';
$newstring = str_replace($find, $replace, $oldstring);
echo $newstring;
I want remove some URL from user input.
Example user input
$input = 'http://www.yahoo.com/';
$input = 'http://yahoo.com/';
$input = 'www.yahoo.com';
$input = 'yahoo.com';
$input = 'http://answers.yahoo.com/question/index;_ylt=AhiI2Ax0jmujpU01wK7W4cLj1KIX;_ylv=3?qid=20110224130854AA9LGdy';
$input = 'yahoo';
$input = 'http://yahoo.com';
$input = 'I love http://yahoo.com';
output I love
Any domain with yahoo.com I want remove the output or return empty result. Maybe more than one domain. Let me know.
Try something like this:
$domains = array('yahoo.com', 'other-domain.org');
$domainsrgx = implode('|', array_map('preg_quote', $domains));
$filtered_userinput = preg_replace('#(^|\s+)(https?://)?([^/\s]*\.)?('.$domainsrgx.')(/[^\s]*)?(?=(\s|$))#is', '', $userinput);
This should remove everything from your example.
$data=str_replace('yahoo.com','',$data);
you can use an array to replace\remove multiple domains
$remove=array('yahoo.com','google.com');
$data=str_replace($remove,'',$data);
Ok. You can do
$rows = split("[\n|\r]", $input);
$output = "";
foreach($rows as $row){
if(strpos($row,"yahoo.com")){
continue;
}else{
$output = $row."\n";
}
}
I hope I'm not missing anything.