I am making a tagging system in which there are user ids separated by comma in database . I call them after words and then break them and get the real user names for those ids . I am using str_replace to replace those words from a status update with their profile links (like facebook tag a friend) . The problem is how to replace multiple tags within the sentence ?
function tags($final_message,$tag_id)
{
GLOBAL $con;
if ($tag_id!==0)
{
$tag_id_explode=explode(",",$tag_id);
foreach($tag_id_explode as $id)
{
$tag_friend_query=mysqli_query($con,"select f_name,l_name from users_profile where user_id='$id'");
$tag_friend_query_result=mysqli_fetch_assoc($tag_friend_query);
$fname=$tag_friend_query_result['f_name'];
$lname=$tag_friend_query_result['l_name'];
$final_name=$fname.' '.$lname;
$html="<a href='profile.php?id=".$id."'>".$final_name."</a>";
}
echo str_replace($final_name,$html,$final_message);
}
}
This only converts one user and if there are multiple it does not work . If i put str_replace inside the loop then it works but it converts at a time and returns two different output with same sentence and one user being converted in each . It should be multiple users converted in just once sentence .
do this
foreach($tag_id_explode as $id)
{
$tag_friend_query=mysqli_query($con,"select f_name,l_name from users_profile where user_id='$id'");
$tag_friend_query_result=mysqli_fetch_assoc($tag_friend_query);
$fname=$tag_friend_query_result['f_name'];
$lname=$tag_friend_query_result['l_name'];
$final_name=$fname.' '.$lname;
$html="<a href='profile.php?id=".$id."'>".$final_name."</a>";
$final_message = str_replace($final_name,$html,$final_message);
}
echo $final_message;
The problem seems here when you use $final_name in str_replace(), it only has one last user's final name. Instead put it in the loop and in $html you are collecting the link, so you may want to do it in this way:
$html .=yourlink;
Please try the string concatinator dot after the variable (.)
Related
I'd like to create a function that makes Behat parse my HTML and tell me how many times he finds a specific text. I found countless ways to do so, but since I'd like to reuse the concept, I can't give him a specific div class where to find it since it could be outside of a div.
This is what I did so far
testfeature.feature
And I should see "CISIA - Arcueil" 2 times
FeatureContext.php
public function iShouldSeeTimes($text, $count) {
$element = $this->getSession()->getPage();
$result = $element->findAll('css', sprintf('div:contains("%s")', $text));
if(count($result) == $count && strpos(reset($result)->getText(), $text)) {
return;
}
else {
throw new ExpectationException('"' . $text . '" was supposed to appear ' . $count . ' times, got ' . count($result) . ' instead', $this->getSession());
}
}
This is a bit messy but I'll tidy all of this up once it works. So far, with this code, I get 19 elements, which are every text contained by every div inside the page I want to check. In a way, I have a possibility to get to my goal, but what I'd like is to directly get what I need (my two occurences) inside $result. However, it looks like I'm doing something wrong with the sprintf() function.
Is there a way to make Behat look for a specific text?
Thank you in advance
Use XPath that matches any type of element with contains instead of css.
For example:
$text = 'my text';
findAll('xpath', "//*[contains(text(), '$text')]");
The second alternative would be to use regular expressions. I recommend the first one.
I am using php/mysql.
I am storing comma separated 6 digit numbers in text based column of table in database.
I want to get the method to find which numbers in given range are present in my database.
My table looks like :
|id|date|commaSeperatedList|
Now as input to sql I want to give a range of number (e.g 234101-234200).
Therefore I am expecting output to be in form of :
|id|date|number|
So, far the solution I have worked on is : Use of PHP's range function.
Using that I created a string based Long Where Clause.
foreach( $words as $word) {
$word=str_pad($word, 6, '0', STR_PAD_LEFT);
$whereClause .= ' commaSeperatedList LIKE "%' . $word . '%" OR';
}
Problem with it is that : I dont get to know exactly which were the numbers that were found common .
e.g lets say List has :
109001,234122,234123,345650
I am giving the range (234101-234200)
The above statement finds all rows that contain any number in provided range . However I also want to know which exact numbers were matched. In my provided example these numbers are : 234122,234123
So, expected output should be :
|1|date|234122|
|1|date|234123|
Any help in this regard will be appreciated.
Eventually had to store data by normalization.
There are queries(Full Text Match) that were doing the work but not only they were complex but slow as well.
As for me , Its always better to store data properly in normalized format. (Its not a space/time tradeOFF).
For others in similar situation . Do some extra work on data and store it properly.
Thanks everyone for valuable answers !
Try This Code May Help You to find between range
<?php
$list = "109001,234122,234123,345650";
$from = "234101";
$to = "234200";
$list = explode(",",$list);
foreach($list as $val)
{
if($val > $from && $val < $to)
{
echo $val."<br>";
}
}
?>
In my database I have one column named name and in each row of that column I'm saving names like this /Mary/Sam/Bob/Michael/. To show the values in my page I need to separate them by breaking the line in each /. Can someone help me?
If I make one echo of my column I will get /Mary/Sam/Bob/Michael/ but I want:
Mary
Sam
Bob
Michael
$names=explode('/', $dbrow['name']);
foreach($names as $aname){
echo $aname.'<br>';
}
Use the explode function with array_filter function
$database_column_string = "/Mary/Sam/Bob/Michael/";
$names = array_filter(explode('/',trim($database_column_string )));
foreach($names as $name){
echo $name;
echo "<br/>";
}
\n may not render a new line in the web browser. So it's better to replace \ with the br tag:
str_replace ("/" , "<br>" , "/Mary/Sam/Bob/Michael/");
The explode function will fix your problem but you should be aware that the way you're using your database is violating the first normal form. Each record should have the same number of attributes.
Here is a guide to the normal forms http://www.bkent.net/Doc/simple5.htm
You should redesign your database to have a table called, for example, Names(id, name)
I have an array element that contains a subpage's content:
$page['content']
I want to do some MySQL query, if the variable's content contains this pattern: {gallery:somerandomIDnumber}
The problem is, I don't want to lose the other stuff, and it's also important that the query should run where the pattern belongs.
For example, this is the $page['content'] content:
<h1>Title of the page</h1>
{gallery:10}
<p>Other informations...</p>
I tried this with preg_match function, but unfortunately I can't figure it out, how I can save the other content around my {gallery:10} pattern.
// Gallery include by ID
preg_match('~\{gallery\:[0-9]{1,5}\}~', $page['content'], $matches);
foreach($matches[0] as $value) {
$int = filter_var($value, FILTER_SANITIZE_NUMBER_INT);
$query = 'SELECT * ';
$query .= 'FROM gallery_images ';
$query .= 'WHERE gid = '.$int;
$gallery = ms_query($query); //ms_query is a function I wrote myself. Unlike mysqli_query function this function doesn't require the connection parameter every single time I call it, only the query itself
while($gimage = mysqli_fetch_assoc($gallery)) {
echo '<img src="admin/uploaded/'.$gimage['imagepath'].'" width ="100">';
}
}
Summarazing, in this situation, I want to echo
the title of the page
some imagepath from my database
other informations
Thanks in return for Your help!
preg match will get you something in $matches only when u put it in ().
e.g. in your {gallery:10}
you will get the 10 if you make your preg like this;
preg_match('~{gallery:([0-9]{1,5})}~', $page['content'], $matches);
also echo your sql query and paste result
I'm trying to write code that will return the past 100 tweets that contain the current trending hashtags on twitter. First I get the contents of the current trends and isolate just trending hashtags:
$json_output=json_decode(file_get_contents("https://api.twitter.com/1/trends/23424977.json"),true);
print_r($json_output);
foreach($json_output[0]['trends'] as $trend) {
if ($trend['name'][0] === '#') {
echo $trend['name'];
$hashtag == $trend['name'];
}
}
But rather than echo the trend['name'], I want to use it to search using the twitter search method. By adding something like this inside the if statement:
$past_uses = json_decode(file_get_contents("http://search.twitter.com/search.json?q="$hashtag"&rpp=100&include_entities=true&result_type=popular"),true);
But the variable $hashtag isn't being defined properly and I don't know why. (When I try to echo $hashtags, to check that it's storing the proper value, it doesn't print anything.) So, what should I change so that the value of $trend['name'] can be used in the URL for the search method in order to get the past tweets that included the trending hashtag?
Thank you!
You're doing a comparison instead of assigning $hashtag.
$hashtag == $trend['name'];
That's basically just saying false inline. Instead, use a single equal sign:
$hashtag = $trend['name'];
Also, for your $past_uses, make sure you concatenate the string with dots properly:
$past_uses = json_decode(
file_get_contents("http://search.twitter.com/search.json?q=" . $hashtag . "&rpp=100&include_entities=true&result_type=popular"),
true);