PHP - read variable from string then replace with variable from Database - php

In my PHP forum i want people to insert pictures just by inserting the number/ID of the picture (which they can see in an online photoalbum)
I am looking for a function that can read a string from their posts for example
"bla bla and look at this amazing picture [IMG]234[/IMG] isn't it awesome ..."
then finds the picture in a database with the ID 234 and replaces [IMG]234[/IMG] with
<img src = "path/to/image.jpg" />
preg_replace wouldn't work :( does anyone have an Idea?
thanks for Your help in advance

I would do it in this way:
$images = array();
$post = preg_replace_callback('|\[img\](\d+)\[/img\]|i', function($matches) use(&$images) {
$images[] = $matches[1];
return '__image_' . $matches[1];
}, $post);
if (count($images)) {
// Select images
$imageIds = implode(',', $images);
// DB query
$res = mysql_query("SELECT id, path FROM post_images WHERE id IN ({$imageIds})") or die(mysql_error());
// Replace
while (($row = mysql_fetch_assoc($res))) {
$post = str_replace('__image_'.$row['id'], '<img src=' . $row['path'] . ' />', $post);
}
}
The advantage is that you make only one query to database. This is always important to minimize them to increase performance. if you don't care or you are sure that the number of images in not going to be too high, you can simple use this code:
$post = preg_replace_callback('|\[img\](\d+)\[/img\]|i', function($matches) {
$res = mysql_query("SELECT path FROM post_images WHERE id = {$matches[1]}") or die(mysql_error());
$path = mysql_result($res, 0);
return "<img src='$path' />";
}, $post);

here is what i did:
$find = preg_match_all("!\[img\][0-9]+\[\/img\]!", $post, $matches);
foreach ($matches as $match) {
foreach ($match as $ma) {
$res = str_replace("[/img]","", str_replace("[img]", "",$ma));
$query = "
SELECT
path
FROM
table
WHERE id = '".$res."'
";
$result = mysql_query($query, $conn) or die(mysql_error());
while($line = mysql_fetch_array($result)) {
$path = $line["path"];
$path = "<img src = '".$path. "'></img>";
$post = str_replace ("[img]" . $res . "[/img]", $path, $post);
}
}
}
note: i don't know why preg_match_all creates a 2 depths array

Your code needs to make a db call to get the path from the ID.
Use a tag parser or basic regex to match \[img][0-9]+\[/img]. (preg_match)
Then query your database for the path with the match result as the ID.
Finally, use str_replace to replace the original "[img]$match[/img]" with
Example (pseudo, look these functions up first)
$matches = preg_match_all('\[img][0-9]+\[/img]', $input);
$output = $input;
foreach ($matches as $match) {
$cur = mysql_query("SELECT path FROM table WHERE ID = $match");
$row = mysql_fetch_array($cur);
$output = str_replace('[img]'.$match.'[/img]',$row['path'],$output);
}
Note: this will only work with tags in lowercasse: img and not with IMG, iMG, ImG and so on.

Why doesn't preg_replace work? Not knowing the the details, I would guess that you are not escaping the square brackets. What does your regular expression look like? Try something like
/\[IMG\](\d+)\[/IMG]/
The image id will then be in the group 1, i.e. $1.
Example:
preg_match_all('/\[IMG\](\d+)\[/IMG]/', $post, $matches);
foreach ($matches as $match) {
echo "Image number: " . $match[1] . "\n";
}

Related

find and replace all occurrences of string [php shortcodes]

i'm using this code to replace shortcodes in a CMS with links including images but it replaces only the first shortcode
$string = $row['Content'];
if(stristr($string,'[gal=')){
$startTag = "[gal=";
$endTag = "]";
$pos1 = strpos($string, $startTag) + strlen($startTag);
$pos2 = strpos($string, $endTag);
$gal = substr($string, $pos1, $pos2-$pos1);
$q=$db->prepare("select * from images where Gal_ID = :gal");
$q->execute(["gal"=>$gal]);
$imgs='';
while($r=$q->fetch(PDO::FETCH_ASSOC)){
$images[] = $r['Image'];
}
foreach($images as $val){
$imgs .= "<a href='gallery/large/$val' class='fancybox-thumbs' rel='gallery'><img src='gallery/thumb/$val'></a>";
}
$result = substr_replace($string, $imgs, $pos1, $pos2-$pos1);
$result = str_replace($startTag,'',$result);
$result = str_replace($endTag,'',$result);
echo $result;
}
else{
echo $string;
}
string contains some paragraphs and 2 shortcodes
[gal=36] and [gal=37]
the result is replacing only the first shortcode with links and images but the second shortcode is displayed like this: "37" just the number. So how to loop through all shortcodes to replace them with links not only the first shortcode
Here is a full example how I described above.
//get matches
if(preg_match_all('/\[gal=(\d+)\]/i', $string, $matches) > 0){
//query for all images. You could/should bind this, but since the expression
//matches only numbers, it is technically not possible to inject anything.
//However best practices are going to be "always bind".
$q=$db->prepare("select Gal_ID, Image from images where Gal_ID in (".implode(',', $matches[1]).")");
$q->execute();
//format the images into an array
$images = array();
while($r=$q->fetch(PDO::FETCH_ASSOC)){
$images[$r['Gal_ID']][] = "<a href='gallery/large/{$r['Image']}' class='fancybox-thumbs' rel='gallery'><img src='gallery/thumb/{$r['Image']}'></a>";
}
//replace shortcode with images
$result = preg_replace_callback('/\[gal=(\d+)\]/i', function($match) use ($images){
if(isset($images[$match[1]])){
return implode('', $images[$match[1]]);
} else {
return $match[0];
}
}, $string);
echo $result;
}
I tested it as much as I could, but I don't have PDO and/or your tables. This should work as a pretty much drop in replacement for what you have above.

How to keep the first filename of each ID from a huge list using PHP or regex?

I have this huge list of filenames in the format car-id_picture-id.jpg like below (only a part of it)
1201_3.jpg
1201_4.jpg
1201_5.jpg
1201_6.jpg
1201_7.jpg
1201_8.jpg
1201_9.jpg
1240_15.jpg
1240_16.jpg
1240_17.jpg
1240_18.jpg
1240_19.jpg
1240_2.jpg
1240_8.jpg
1240_9.jpg
1511_0.jpg
1511_1.jpg
1511_7.jpg
1511_8.jpg
What I want is
1201_3.jpg
1240_15.jpg
1511_0.jpg
My aim is using a php script or even a regex in Notepad++ to have a list of only one image per carID and preferrably the first on the list of each car.
Is there a way to do this?
Thank you
You could make use of the below regex with global and multiline flags
^((\d{4})_\d+.jpg)(\n\2.+)+
and replace with $1 or you could take the captured value from group 1.
DEMO
This will do it:
<?php
$images = "
1201_3.jpg
1201_4.jpg
1201_5.jpg
1201_6.jpg
1201_7.jpg
1201_8.jpg
1201_9.jpg
1240_15.jpg
1240_16.jpg
1240_17.jpg
1240_18.jpg
1240_19.jpg
1240_2.jpg
1240_8.jpg
1240_9.jpg
1511_0.jpg
1511_1.jpg
1511_7.jpg
1511_8.jpg
";
$images_array = array_filter(explode("\n", $images));
$output = array();
foreach ($images_array as $img) {
$matches = array();
preg_match('%([^_-]+)_.*%', $img, $matches);
$car_id = $matches[1];
if(isset($output[$car_id])) continue;
$output[$car_id] = $img;
}
var_dump($output);
Maybe you should consider if you really need regex...
Sometimes is preferable (e.g. in terms of processing time) to use string functions. For instance, something like:
$s = file("images.txt");
$array = array();
foreach($s as $line) {
$carId = substr($line,0,strpos($line,"_"));
if(!array_key_exists($carId,$array)) {
$array[$carId] = trim($line);
}
}
var_dump(implode("\n",$array));
You can do this.
$images = "
1201_3.jpg
1201_8.jpg
1201_9.jpg
1240_15.jpg
1240_16.jpg
1511_0.jpg
1511_1.jpg
";
$array = array();
$foo = explode('.jpg', $images);
foreach($foo as $bar) {
$digi = substr(trim($bar), 0,4) . "<br />";
if(!in_array($digi, $array)) {
array_push($array, $digi);
echo $bar . ".jpg <br/>";
}
}
//ouput
1201_3.jpg
1240_15.jpg
1511_0.jpg

Regex not quite right

I have a site crawler which displays a list of urls, but the problem is I cannot for the life of me get the last regex quite right.
all urls end up listed as:
http://www.website.org/page1.html&--EFTTIUGJ4ITCyh0Frzb_LFXe_eHw
http://website.net/page2/&--EyqBLeFeCkSfmvA7p0cLrsy1Zm1g
http://foobar.website.com/page3.php&--E5WRBxuTOQikDIyBczaVXveOdRFg
The Urls can all be different and the only thing which seems static is the & symbol.
How would go abouts getting rid of the & symbol and everything beyond it to the right?
Here is what I have tried with the above results:
function getresults($sterm) {
$html = file_get_html($sterm);
$result = "";
// find all span tags with class=gb1
foreach($html->find('h3[class="r"]') as $ef)
{
$result .= $ef->outertext . '<br>';
}
return $result;
}
function geturl($url) {
$var = $url;
$result = "";
preg_match_all ("/a[\s]+[^>]*?href[\s]?=[\s\"\/url?q=\']+".
"(.*?)[\"\']+.*?>"."([^<]+|.*?)?<\/a>/",
$var, $matches);
$matches = $matches[1];
foreach($matches as $var)
{
$result .= $var."<br>";
}
echo preg_replace('/sa=U.*?usg=.*?AFQjCN/', "--" , $result);
}
if url are ALWAYS in the same format, use explode :
<?php
$tmp = explode("&", "http://foobar.website.com/page3.php&--E5WRBxuTOQikDIyBczaVXveOdRFg");
?>
$tmp[0] should content "http://foobar.website.com/page3.php" and
$tmp[1] should content "--E5WRBxuTOQikDIyBczaVXveOdRFg"
A simple way to remove everything after the & character:
$result = substr($result, 0, strpos($result, '&'));

Extracting Twitter hashtag from string in PHP

I need some help with twitter hashtag, I need to extract a certain hashtag as string variable in PHP.
Until now I have this
$hash = preg_replace ("/#(\\w+)/", "#$1", $tweet_text);
but this just transforms hashtag_string into link
Use preg_match() to identify the hash and capture it to a variable, like so:
$string = 'Tweet #hashtag';
preg_match("/#(\\w+)/", $string, $matches);
$hash = $matches[1];
var_dump( $hash); // Outputs 'hashtag'
Demo
I think this function will help you:
echo get_hashtags($string);
function get_hashtags($string, $str = 1) {
preg_match_all('/#(\w+)/',$string,$matches);
$i = 0;
if ($str) {
foreach ($matches[1] as $match) {
$count = count($matches[1]);
$keywords .= "$match";
$i++;
if ($count > $i) $keywords .= ", ";
}
} else {
foreach ($matches[1] as $match) {
$keyword[] = $match;
}
$keywords = $keyword;
}
return $keywords;
}
As i understand you are saying that
in text/pargraph/post you want to show tag with hash sign(#) like this:- #tag
and in url you want to remove # sign because the string after # is not sended to server in request so i have edited your code and try out this:-
$string="www.funnenjoy.com is best #SocialNetworking #website";
$text=preg_replace('/#(\\w+)/','<a href=/hash/$1>$0</a>',$string);
echo $text; // output will be www.funnenjoy.com is best <a href=search/SocialNetworking>#SocialNetworking</a> <a href=/search/website>#website</a>
Extract multiple hashtag to array
$body = 'My #name is #Eminem, I am rap #god, #Yoyoya check it #out';
$hashtag_set = [];
$array = explode('#', $body);
foreach ($array as $key => $row) {
$hashtag = [];
if (!empty($row)) {
$hashtag = explode(' ', $row);
$hashtag_set[] = '#' . $hashtag[0];
}
}
print_r($hashtag_set);
You can use preg_match_all() PHP function
preg_match_all('/(?<!\w)#\w+/', $description, $allMatches);
will give you only hastag array
preg_match_all('/#(\w+)/', $description, $allMatches);
will give you hastag and without hastag array
print_r($allMatches)
You can extract a value in a string with preg_match function
preg_match("/#(\w+)/", $tweet_text, $matches);
$hash = $matches[1];
preg_match will store matching results in an array. You should take a look at the doc to see how to play with it.
Here's a non Regex way to do it:
<?php
$tweet = "Foo bar #hashTag hello world";
$hashPos = strpos($tweet,'#');
$hashTag = '';
while ($tweet[$hashPos] !== ' ') {
$hashTag .= $tweet[$hashPos++];
}
echo $hashTag;
Demo
Note: This will only pickup the first hashtag in the tweet.

How to wrap user mentions in a HTML link on PHP?

Im working on a commenting web application and i want to parse user mentions (#user) as links. Here is what I have so far:
$text = "#user is not #user1 but #user3 is #user4";
$pattern = "/\#(\w+)/";
preg_match_all($pattern,$text,$matches);
if($matches){
$sql = "SELECT *
FROM users
WHERE username IN ('" .implode("','",$matches[1]). "')
ORDER BY LENGTH(username) DESC";
$users = $this->getQuery($sql);
foreach($users as $i=>$u){
$text = str_replace("#{$u['username']}",
"<a href='#' class='ct-userLink' rel='{$u['user_id']}'>#{$u['username']}</a> ", $text);
}
$echo $text;
}
The problem is that user links are being overlapped:
<a rel="11327" class="ct-userLink" href="#">
<a rel="21327" class="ct-userLink" href="#">#user</a>1
</a>
How can I avoid links overlapping?
Answer Update
Thanks to the answer picked, this is how my new foreach loop looks like:
foreach($users as $i=>$u){
$text = preg_replace("/#".$u['username']."\b/",
"<a href='#' title='{$u['user_id']}'>#{$u['username']}</a> ", $text);
}
Problem seems to be that some usernames can encompass other usernames. So you replace user1 properly with <a>user1</a>. Then, user matches and replaces with <a><a>user</a>1</a>. My suggestion is to change your string replace to a regex with a word boundary, \b, that is required after the username.
The Twitter widget has JavaScript code to do this. I ported it to PHP in my WordPress plugin. Here's the relevant part:
function format_tweet($tweet) {
// add #reply links
$tweet_text = preg_replace("/\B[#@]([a-zA-Z0-9_]{1,20})/",
"#<a class='atreply' href='http://twitter.com/$1'>$1</a>",
$tweet);
// make other links clickable
$matches = array();
$link_info = preg_match_all("/\b(((https*\:\/\/)|www\.)[^\"\']+?)(([!?,.\)]+)?(\s|$))/",
$tweet_text, $matches, PREG_SET_ORDER);
if ($link_info) {
foreach ($matches as $match) {
$http = preg_match("/w/", $match[2]) ? 'http://' : '';
$tweet_text = str_replace($match[0],
"<a href='" . $http . $match[1] . "'>" . $match[1] . "</a>" . $match[4],
$tweet_text);
}
}
return $tweet_text;
}
instead of parsing for '#user' parse for '#user ' (with space in the end) or ' #user ' to even avoid wrong parsing of email addresses (eg: mailaddress#user.com) maybe ' #user: ' should also be allowed. this will only work, if usernames have no whitespaces...
You can go for a custom str replace function which stops at first replace.. Something like ...
function str_replace_once($needle , $replace , $haystack){
$pos = strpos($haystack, $needle);
if ($pos === false) {
// Nothing found
return $haystack;
}
return substr_replace($haystack, $replace, $pos, strlen($needle));
}
And use it like:
foreach($users as $i=>$u){
$text = str_replace_once("#{$u['username']}",
"<a href='#' class='ct-userLink' rel='{$u['user_id']}'>#{$u['username']}</a> ", $text);
}
You shouldn’t replace one certain user mention at a time but all at once. You could use preg_split to do that:
// split text at mention while retaining user name
$parts = preg_split("/#(\w+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
$n = count($parts);
// $n is always an odd number; 1 means no match found
if ($n > 1) {
// collect user names
$users = array();
for ($i=1; $i<$n; $i+=2) {
$users[$parts[$i]] = '';
}
// get corresponding user information
$sql = "SELECT *
FROM users
WHERE username IN ('" .implode("','", array_keys($users)). "')";
$users = array();
foreach ($this->getQuery($sql) as $user) {
$users[$user['username']] = $user;
}
// replace mentions
for ($i=1; $i<$n; $i+=2) {
$u = $users[$parts[$i]];
$parts[$i] = "<a href='#' class='ct-userLink' rel='{$u['user_id']}'>#{$u['username']}</a>";
}
// put everything back together
$text = implode('', $parts);
}
I like dnl solution of parsing ' #user', but maybe is not suitable for you.
Anyway, did you try to use strip_tags function to remove the anchor tags? That way you have the string without the links, and you can parse it building the links again.
strip_tags

Categories