I'm trying to parse a string with a php function to it will echo an <img> tag for the bbcode style :smiley: string.
It does work put only parses the first item found and then stops the loop.
I need it to parse the whole text and then return it.
I tried the following:
function parse_emoji($string){
$possibleCodes = array('+1','-1','angry','anguished','astonished','bawling','bleep_bloop','blush','cold_swaet','confounded','confused','cookie','cry','dissapointed','dissapointed_relieved','dizzy','expressionless','fearfull','flushed','frown','grey_question','grimace','grin','heart','heartbreak','hushed','innocent','joy','kiss','kissing_blush','kissing_closed_eyes','kissing_smiling_eyes','kissing_wink','lol','love','love','mask','mrgreen','naughty','neutral','no_mouth','open_mouth','pensive','persevere','rage','relaxed','relieved','scream','skull','sleeping','sleepy','smile','smiley','smirk','star','tongue_grin','tongue_wink','triumph','trollface','unamused','warning','weary','wink','worried','yum');
foreach($possibleCodes as $code) {
return str_replace(':'.$code.':', "<img class='smiley' src='".asset_url()."img/emoticons/".$code.".png'>", $string);
}
}
If I then parse $string('this is a smiley :smile: and this one too :smile:') it will insert an image for the first tag but will echo plain :smiley: for the second.
It's because you return a result in the foreach. It will stop the loop and return the result after only one loop occurence.
You should remove the return in the foreach statement :
function parse_emoji($string){
$possibleCodes = array(...);
foreach($possibleCodes as $code) {
$string = str_replace(':'.$code.':', "<img class='smiley' src='".asset_url()."img/emoticons/".$code.".png'>", $string);
}
return $string;
}
Because you're returning after the first replace.... loop over all the individual replacements, and only then return
function parse_emoji($string){
$possibleCodes = array('+1','-1','angry','anguished','astonished','bawling','bleep_bloop','blush','cold_swaet','confounded','confused','cookie','cry','dissapointed','dissapointed_relieved','dizzy','expressionless','fearfull','flushed','frown','grey_question','grimace','grin','heart','heartbreak','hushed','innocent','joy','kiss','kissing_blush','kissing_closed_eyes','kissing_smiling_eyes','kissing_wink','lol','love','love','mask','mrgreen','naughty','neutral','no_mouth','open_mouth','pensive','persevere','rage','relaxed','relieved','scream','skull','sleeping','sleepy','smile','smiley','smirk','star','tongue_grin','tongue_wink','triumph','trollface','unamused','warning','weary','wink','worried','yum');
foreach($possibleCodes as $code) {
$string = str_replace(':'.$code.':', "<img class='smiley' src='".asset_url()."img/emoticons/".$code.".png'>", $string);
}
return $string;
}
For some reason I'm getting the word 'array' as an output when I try to do a foreach to echo out the values in an array (there are 2 values). These show fine if I use print_r in the array so I know they are there. I've also tried using as list but that only shows the first value and nothing after it.
It's getting late so it might be something pretty silly! Thanks in advance
<?php
$crawl_url = "./emails.php";
function get_email($url) {
$input = #file_get_contents($url);
$regexp = '/[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b/i';
preg_match_all($regexp, $input, $matches);
if (empty($input)) {
echo "No email addresses found";
}
else {
foreach($matches as $matches_values) {
print $matches_values;
}
}
}
get_email($crawl_url);
echo '<br /> function complete';
?>
I think you're missing the 0 index on $matches.
Try this :
foreach($matches[0] as $matches_values) {
print $matches_values;
}
Here is the situation. I have 2 files
content.php
<?php $my_var = "this is a variable"; ?>
<h1> php{my_var} </h1>
index.php
<?php include "content.php" ?>
The result should be:
<h1>this is a variable</h1>
I know how to work with preg_replace_callback. But I don't know how can I change php{my_var} with the value of $my_var.
All the logic should happens inside the index.php.
Edit
index.php
function replace_pattern($match)
{
what should I write here
}
echo preg_replace_callback("/php\:\{(.*)\}/", "replace_pattern", $Content);
Edit 2
Variables are not declare in the global scope
Note the added question-mark in the regular expression to make it less greedy.
$my_var = 'Hello World!';
// Get all defined variables
$vars = get_defined_vars();
$callback = function($match) use ($vars)
{
$varname = $match[1];
if (isset($vars[$varname])) {
return $vars[$varname]; // or htmlspecialchars($vars[$varname]);
} else {
return $varname . ' (doesn\'t exists)';
}
};
echo preg_replace_callback("/php\:\{(.*?)\}/", $callback, $Content);
Demo: http://phpfiddle.org/main/code/ax15-bpyw
Try this:
$my_var = "this is a variable";
$string = '<h1> php{my_var} </h1>';
$pattern = '/php{(.*)}/i';
preg_match($pattern, $string, $match);
$varName = $match[1];
$newString= preg_replace($pattern, $$varName, $string);
echo $newString;
But, warning!
In this case, i assume, if the code is php{somethin}, then there should be a $something variable. I used dynamic variable name. If you want to use only $my_var then use like this:
$newString= preg_replace($pattern, $my_var, $string);
I have seen some samples using var_dump, but I I would rather use a simple echo, if it's possible.
It should look like this using echo:
This is a
simple text
I just wrote
Using var_dump:
function split3($text)
{
$array = array();
foreach(explode(' ',$text) as $i=>$word)
{
if($i%3) {
$array[floor($i/3)] .= ' '.$word;
} else {
$array[$i/3] = $word;
}
}
return $array;
}
$text = "This is a simple text I just wrote";
var_dump(split3($text));
Your sample output is a bit wrong compare to your question.
If the output is like this.
This is a
simple text I
just wrote
Then replace the var_dump(split3($text)); with this
$splitedText = split3($text);
foreach($splitedText as $value){ //Just print the array content
echo $value . "<br />"; //I use <br /> as a new line
}
<?php
$str = "asd,ad";
if(preg_match(",",$str)) {
echo "ok";
}
?
It outputs me
No ending delimiter ',' found in....
?>
your pattern can be replaced to strpos instead
if(strpos($str, ",")!==false)
{
echo "ok";
}
You are missing delimitters, try this:
$str = "asd,ad";
if(preg_match("/,/",$str)) {
echo "ok";
}
To find out more instances, you may want to use preg_match_all function too.