I would like to make a [code][/code] tag for bbcode so that what would be inside wouldn't be taken into account by the php regex that I made.
Example :
Hello [b]newbie[/b], to write in bold, use the following : [code][b](YOURTEXT)[/b][/code]
Should return in HTML :
Hello <strong>newbie</strong>, to write in bold, use the following : [b](YOURTEXT)[/b]
Here is a view of a part of my bbcode function :
<?
function bbcode($var) {
$var = preg_replace('`\[b\](.+)\[/b\]`isU', '<strong>$1</strong>', $var);
$var = preg_replace('`\[i\](.+)\[/i\]`isU', '<em>$1</em>', $var);
$var = preg_replace('`\[u\](.+)\[/u\]`isU', '<u>$1</u>', $var);
return $var;
}
?>
Thank you in advance for your kind help !
EDIT :
Here is how I finally made it work :
<?
function bbcode($var) {
$var2 = preg_split('`(\[code].*?\[/code])`isU', $var, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$var = preg_replace('`\[b\](.+)\[/b\]`isU', '<strong>$1</strong>', $var);
$var = preg_replace('`\[i\](.+)\[/i\]`isU', '<em>$1</em>', $var);
$var = preg_replace('`\[u\](.+)\[/u\]`isU', '<u>$1</u>', $var);
$var = preg_replace('`(\[code].*?\[/code])`isU', $var2[1], $var);
$var = preg_replace('`\[code\](.+)\[/code\]`isU', '<div>$1</div>', $var);
return $var;
}
$text = 'Hello [b]newbie[/b], to write in bold, use the following [u]lol[/u] : [code][b](YOURTEXT) [u]lol[/u][/b][/code] [b][u]LOL[/u][/b]';
echo bbcode($text);
?>
HOWEVER, there is a new problem left : if the character chain starts directly with '[code]' for example
[code][b]hello[/b][/code] test
than the result will be :
test test
This is because $var2[1] now leads to what comes after the [/code].
Could someone please help me to make a better delimitation that would also work for that second character chain ? Thank you in advance !
Finally, I solve all the problems I had with that :
<?
function bbcode($var) {
$var2 = getStringBetween($var, '[code]', '[/code]');
$var = preg_replace('`\[b\](.+)\[/b\]`isU', '<strong>$1</strong>', $var);
$var = preg_replace('`\[i\](.+)\[/i\]`isU', '<em>$1</em>', $var);
$var = preg_replace('`\[u\](.+)\[/u\]`isU', '<u>$1</u>', $var);
$var = preg_replace('`(\[code].+\[/code])`isU', '<div>'.$var2.'</div>', $var);
return $var;
}
function getStringBetween($str,$from,$to)
{
$sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
return substr($sub,0,strpos($sub,$to));
}
$text = 'Hello [b]newbie[/b], to write in bold, use the following [u]lol[/u] : [code][b](YOURTEXT) [u]lol[/u][/b][/code] [b][u]LOL[/u][/b]';
echo bbcode($text);
?>
Related
I'm trying to lowercase every character in a string except for the last one that should be in uppercase.
Here is my code:
function caps_caps($var) {
$var = strrev(ucwords(strrev($var)));
echo $var;
}
caps_caps("HeLlo WOrld"); // should returns "hellO worlD"
This is the easy solution of this problem
function caps_caps($var) {
$var = strrev(ucwords(strrev(strtolower($var))));
echo $var;
}
caps_caps("HeLlo WOrld");
Demo
You also need to convert the string to lowercase first.
function caps_caps($var) {
$var = strrev(ucwords(strrev(strtolower($var))));
echo $var;
}
caps_caps("HeLlo WOrld"); // returns "hellO worlD"
function caps_caps($text) {
$value_to_print = '';
$text = strrev(ucwords(strrev($text)));
$words = explode(' ', $text);
foreach($words as $word){
$word = strtolower($word);
$word[strlen($word)-1] = strtoupper($word[strlen($word)-1]);
$value_to_print .= $word . ' ';
}
echo trim($value_to_print);
}
caps_caps("HeLlo WOrld");
You can try this piece of code.
function uclast($s)
{
$lastCharacterUppar = '';
if ( preg_match('/\s/',$s) ){//If string has space
$explode = explode(' ',$s);
for($i=0;$i<count($explode);$i++){
$l=strlen($explode[$i])-1;
$explode[$i] = strtolower($explode[$i]);
$explode[$i][$l] = strtoupper($explode[$i][$l]);
}
$lastCharacterUppar = implode(' ', $explode);
} else { //if string without space
$l=strlen($s)-1;
$s = strtolower($s);
$s[$l] = strtoupper($s[$l]);
$lastCharacterUppar = $s;
}
return $lastCharacterUppar;
}
$str = 'hey you yo';
echo uclast($str);
Try this, you forgot to do foreach, each elements.
function uclast_words($text, $delimiter = " "){
foreach(explode($delimiter, $text) as $value){
$temp[] = strrev(ucfirst(strrev(strtolower($value))));
}
return implode($delimiter, $temp);
}
print_r(uclast_words("hello world", " "));
I hope this is the answer of your question.
Here is a multibyte safe technique that performs the title-casing with one call instead of two. The string reversal and re-reversal is still necessary.
Code: (Demo)
echo strrev(
mb_convert_case(
strrev('HeLlo WOrld'),
MB_CASE_TITLE
)
);
// hellO worlD
$string = 'Hello [user=1]';
$bbc = array('/\[user=(.*?)\]/is');
$replace = array(user('textlink',$1));
$s = preg_replace($bbc , $replace, $string);
echo $s
How do I change $1 in preg_replace with a function?
If I understand you correctly, you want to execute user() function on each match? As mentioned in comments, use the preg_replace_callback() function.
<?php
$string = 'Hello [user=1]';
$s = preg_replace_callback(
'/\[user=(.*?)\]/is',
function($m) {return user('textlink', $m[1]);},
$string
);
echo $s;
You may use a preg_replace_callback_array:
$string = 'Hello [user=1]';
$bbc = array(
'/\[user=(.*?)\]/is' => function ($m) {
return user('textlink',$m[1]);
});
$s = preg_replace_callback_array($bbc, $string);
echo $s;
See PHP demo.
You may add up the patterns and callbacks to $bbc.
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'm using str_replace() PHP function which doesn't replace empty string.
Is it possible to replace it using some other PHP functions?
Here is my code:
$var = "text1|text2";
$expn = explode("|", $var);
$new = "new text";
str_replace($expn[1], $new, $var);
This code really works, but if the second value is empty it doesn't:
$var = "text1|";
$expn = explode("|", $var);
$new = "new text";
str_replace($expn[1], $new, $var);
I want this to echo text1|new text but it doesnt. In the first case it does without a problem. I want this to be changed anyway, it doesn't depend if it's empty or not.
Thanks in advance.
Try this after your explode():
if ($expn[1] == '') {
$var .= 'new text';
} else {
str_replace($expn[1], $new, $var);
}
I am trying to find the word and add a number next to it. How could he do? I tried with the code below, but I could not. Could anyone help me?
Thank you!
$string = 'I220ABCD I220ABCDEF I220ABCDEFG'
if (preg_match("/I220.*/", $string, $matches)) {
echo $matches[0];
}
Expected result:
I220ABCD9
I220ABCDEF10
I220ABCDEFG11
Use preg_replace_callback instead like this:
$str = 'I220AB FRRRR CD I221ABCDEF I220AB DSFDSF CDEFG';
$repl= preg_replace_callback('~(I220[^\s]+)~', function($m) {
static $i=9;
return $m[1] . $i++;
}, $str);
echo $repl\n"; // I220AB9 FRRRR CD I221ABCDEF I220AB10 DSFDSF CDEFG
I dont know what your requirnments for adding the number at the end are so i just incremeneted during the loop;
$string = 'I220ABCD I220ABCDEF I220ABCDEFG';
$arrayStrings = explode(" ", $string);
$int = 9;
$newString = '';
foreach($arrayStrings as $stringItem)
{
if (preg_match("/I220.*/", $stringItem, $matches))
{
$stringItem = $stringItem.$int;
$newString = $newString.$stringItem." ";
$int++;
}
}
echo $newString;
Use preg_replace_callback():
$string = 'I220ABCD I220ABCDEF I220ABCDEFG';
// This requires PHP5.3+ since it's using an anonymous function
$result = preg_replace_callback('/I220[^\s]*/', function($match){
return($match[0].rand(0,10000)); // Add a random number between 0-10000
}, $string);
echo $result; // I220ABCD3863 I220ABCDEF5640 I220ABCDEFG989
Online demo.
You'll need to use a catch block in your regex e.g. "/I220([^ ]+)/" and if you want them all, you'll need to use preg_match_all, too.
preg_replace_callback with your needs:
$string = 'I220ABCD I220ABCDEF I220ABCDEFG';
class MyClass{
private static $i = 9;
private static function callback($matches){
return $matches[0] . self::$i++;
}
public static function replaceString($string){
return preg_replace_callback('/I220[^\s]+/',"self::callback",$string);
}
}
echo(MyClass::replaceString($string));
of course you can edit to class to initialize the way you want