I am currently working on a Webproject for my school which is build with HTML, PHP and SQL Databases for dynamic content. Until now everything works out pretty good but I have reached a point where I have to echo something out which contains many Characters like '' and "" which pretty much makes it impossible to use PHP echo with those starting tags ('' and ""). Is there any other way to start a PHP echo ?
if ($rows[$number]['kulturschule'] == 1) {
echo '<div class="tp-caption tp-resizeme hover-scale"
data-x="center"
data-y="center"
data-voffset="[290, 290, 250, 210]"
data-hoffset="0"
data-frames='[{"delay":1000,"speed":2000,"frame":"0","from":"sX:0.9;sY:0.9;opacity:0;fb:20px;","to":"o:1;fb:0;","ease":"Power3.easeInOut"},{"delay":"wait","speed":500,"frame":"999","to":"sX:0.9;sY:0.9;opacity:0;fb:20px;","ease":"Power3.easeInOut"}]'
style="z-index: 20; max-width: auto; max-height: auto; white-space: nowrap;"><img src="img/logo/kulturschule.jpg"> ';
This is a perfect situation to use HEREDOC:
// put all the html in a variable:
$html = <<<EOT
<div class="tp-caption tp-resizeme hover-scale"
data-x="center"
data-y="center"
data-voffset="[290, 290, 250, 210]"
data-hoffset="0"
data-frames='[{"delay":1000,"speed":2000,"frame":"0","from":"sX:0.9;sY:0.9;opacity:0;fb:20px;","to":"o:1;fb:0;","ease":"Power3.easeInOut"},{"delay":"wait","speed":500,"frame":"999","to":"sX:0.9;sY:0.9;opacity:0;fb:20px;","ease":"Power3.easeInOut"}]'
style="z-index: 20; max-width: auto; max-height: auto; white-space: nowrap;"><img src="img/logo/kulturschule.jpg">
EOT;
// note, that EOT; has to be at the very start of the line.
// then:
echo $html;
I have reached a point where I have to echo something out which contains many Characters like '' and "" which pretty much makes it impossible to use PHP echo with those starting tags
Then you should escape those characters inside. cf. http://php.net/string
When you look for an alternative, you can use HEREDOC/NOWDOC syntax (see link above).
Yes you have the possiblity to use this format:
$text = <<<EOT
Place your text between the EOT. It's
the delimiter that ends the text
of your multiline string.
$var
EOT;
If you want to use raw strings use this format:
$var = "foo";
$text = <<<'EOT'
My $var
EOT;
This will ignore the $var and print it as-is
Note:
You can not indent the EOT;
Another solution is to go way old-school and use php like it was used 10 years ago:
<?php
if ($rows[$number]['kulturschule'] == 1) {
?>
<div class="tp-caption tp-resizeme hover-scale"
data-x="center"
data-y="center"
data-voffset="[290, 290, 250, 210]"
data-hoffset="0"
data-frames='[{"delay":1000,"speed":2000,"frame":"0","from":"sX:0.9;sY:0.9;opacity:0;fb:20px;","to":"o:1;fb:0;","ease":"Power3.easeInOut"},{"delay":"wait","speed":500,"frame":"999","to":"sX:0.9;sY:0.9;opacity:0;fb:20px;","ease":"Power3.easeInOut"}]'
style="z-index: 20; max-width: auto; max-height: auto; white-space: nowrap;"><img src="img/logo/kulturschule.jpg">
<?php } ?>
You can use the following way to escape quotes inside echo :
if ($rows[$number]['kulturschule'] == 1) {
echo "<div class='tp-caption tp-resizeme hover-scale'
data-x='center'
data-y='center'
data-voffset='[290, 290, 250, 210]'
data-hoffset='0'
data-frames='[{\"delay\":1000,\"speed\":2000,\"frame\":\"0\",\"from\":\"sX:0.9;sY:0.9;opacity:0;fb:20px;\",\"to\":\"o:1;fb:0;\",\"ease\":\"Power3.easeInOut\"},{\"delay\":\"wait\",\"speed\":500,\"frame\":\"999\",\"to\":\"sX:0.9;sY:0.9;opacity:0;fb:20px;\",\"ease\":\"Power3.easeInOut\"}]";
You can escape charachters:
\' single quote
\" double quote
Related
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php regex [b] to <b>
I'm having trouble with regex, I'm an absolute regex noob. I can't see what's going wrong with trying to convert the HTML back to the 'BBCode'.
Could somebody take a look at the 'unquote' function and tell me the obvious mistake I'm making? (I know it's obvious because I always find the un-obvious errors)
NOTE: I'm not using recursive Regex because I couldn't get my head around it and already started this way round of sorting out the Quotes so they're nested.
<?php
function quote($str){
$str = preg_replace('#\[(?i)quote=(.*?)\](.*?)#si', '<div class="quote"><div class="quote-title">\\1 wrote:</div><div class="quote-inner">\\2', $str);
$str = preg_replace('#\[/(?i)quote\]#si', '</div></div>', $str);
return $str;
}
function unquote($str){
$str = preg_replace('#\<(?i)div class="quote"\>\<(?i)div class="quote_title"\>(.*?)wrote:\</(?i)div\><(?i)div class="quote-inner"\>(.*?)#si', '[quote=\\1]\\2', $str);
$str = preg_replace('#\</(?i)div\></(?i)div\>#si', '[/quote]', $str);
}
?>
This is just some code to help test it:
<html>
<head>
<style>
body {
font-family: sans-serif;
}
.quote {
background: rgba(51,153,204,0.4) url(../img/diag_1px.png);
border: 1px solid rgba(116,116,116,0.36);
padding: 5px;
}
.quote-title, .quote_title {
font-size: 18px;
margin: 5px;
}
.quote-inner {
margin: 10px;
}
</style>
</head>
<body>
<?php
$quote_text = '[quote=VCMG][quote=2xAA]DO RECURSIVE QUOTES WORK?[/quote]I have no idea.[/quote]';
$quoted = quote($quote_text);
echo $quoted.'<br><br>'.unquote($quoted); ?>
</body>
Thanks in advance, Sam.
Well, you could start by setting you php class to either quote-title or quote_title but keep it consistent.
Then, add a return $str; to your second function and you should be nearly there.
And you can simplify your regex's a little :
function quote($str){
$str = preg_replace('#\[quote=(.*)\]#siU', '<div class="quote"><div class="quote-title">\\1 wrote:</div><div class="quote-inner">', $str);
$str = preg_replace('#\[/quote\]#si', '</div></div>', $str);
return $str;
}
function unquote($str){
$str = preg_replace('#<div class="quote"><div class="quote-title">(.*) wrote:</div><div class="quote-inner">#siU', '[quote=\\1]', $str);
$str = preg_replace('#</div></div>#si', '[/quote]', $str);
return $str;
}
But beware of replacing with different calls the start and end tags of your quotes. I thin that unquote can create some strange behaviours if you happen to have other bbcode creating </div></div> code.
Personally, I take advantage of the fact that the resulting HTML is basically:
<div class="quote">Blah <div class="quote">INCEPTION!</div> More blah</div>
Repeatedly run the regex until there are no more matches:
do {
$str = preg_replace( REGEX , REPLACE , $str , -1 , $c);
} while($c > 0);
Also, do it as one regex to make this easier:
'(\[quote=(.*?)\](.*?)\[/quote\])is'
'<div class="quote"><div class="quote-title">$1 wrote:</div><div class="quote-inner">$1</div></div>'
I have some html links, like
urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser
and I need to cut the text (not the link) if it's longer more than some characters (let's say 30). So the link must become somethings like :
urlThatIne... ...TheBrowser
How can I do it? Regex? I'm using PHP and jQuery (but I'd like to do it server side).
There probably is a regex method, but I'd opt for a simple substr().
if (strlen($title)>30) {
$title=substr($title, 0, 10) . "..." . substr($title, -10);
}
Just use css.
urlThatIne... ...TheBrowser
a {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
width: 300px;
-o-text-overflow: ellipsis;
-ms-text-overflow: ellipsis;
}
As far as I understand, you are not creating this page but parsing it. So first, I would suggest using a dom parser (I use: http://simplehtmldom.sourceforge.net/), get the anchor element's inner text. Than use substr ( http://tr.php.net/manual/en/function.substr.php ) method to cut it.
If you want the link text to indicate that it has been truncated...
function printLink( $url ){
$text = strlen( $url ) > 30
? substr( $url, 30 ) . '…'
: $url;
echo '' . $text . '';
}
printLink( 'urlThatIneedToCutBecauseItIsTooLongAndWithoutEmptySpaceItDoestGetANewLineWhenItIsPrintedByTheBrowser' );
// urlThatIneedToCutBecauseItIsT…
Hi im using a pretty basic bbcode parser.
could you guys help me with a problem of mine?
but when for example this is written:
[quote=tanab][quote=1][code]a img{
text-decoration: none;
}[/code][/quote][/quote]
the output is this:
tanab said:
[quote=1]
a img{
text-decoration: none;
}
[/quote]
how would i go and fix that? im realllly bad at the whole preg_replace stuff.
this is my parser:
function bbcode($input){
$input = htmlentities($input);
$search = array(
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[img\](.*?)\[\/img\]/is',
'/\[url=(.*?)\](.*?)\[\/url\]/is',
'/\[code\](.*?)\[\/code\]/is',
'/\[\*\](.*?)/is',
'/\\t(.*?)/is',
'/\[quote=(.*?)\](.*?)\[\/quote\]/is',
);
$replace = array(
'<b>$1</b>',
'<i>$1</i>',
'<img src="$1">',
'$2',
'<div class="code">$1</div>',
'<ul><li>$1</li></ul>',
' ',
'<div class="quote"><div class="quote-writer">$1 said:</div><div class="quote-body">$2</div></div>',
);
return preg_replace($search,$replace,$input);
}
This could be adapted with a recursive regex:
'/\[quote=(.*?)\](((?R)|.*?)+)\[\/quote\]/is'
Which will at least ensure that the output divs will not be incorrectly nested. But you would still have to run the regex twice or three times to catch all quote blocks.
Otherwise it would require a rewrite of your code with preg_replace_callback. Which I cannot be bothered to showcase, since this came up a few dozen times already (try the site search!), has been solved before, etc.
I found lots of posts regarding estracting a filename from an img-tag, but none from a CSS inline style tag. Here's the source string
<span style="width: 40px; height: 30px; background-image: url("./files/foo/bar.png");" class="bar">FOO</span>
What I want to get is bar.png.
I tried this:
$pattern = "/background-image: ?.png/";
preg_match($pattern, $string, $matches);
But this didnt work out.
Any help appreciated..
You need to read up about regular expressions.
"/background-image: ?.png/"
means "background-image:" followed optionally by a space, followed by any single character, followed (directly) by "png".
Exactly what you need depends on how much variation you need to allow for in the layout of the tag, but it will be something like
"/background-image\s*:\s*url\s*(\s*".*([^\/]+)"/
where all the "\s*" are optional spaces, and parenthesis captures something that doesn't contain a slash.
Generally, regexp is not a good tool for parsing HTML, but in this limited case it might be OK.
$string = '<span style="width: 40px; height: 30px; background-image: url("./files/foo/bar.png");" class="bar">FOO</span>';
$pattern = '/background-image:\s*url\(\s*([\'"]*)(?P<file>[^\1]+)\1\s*\)/i';
$matches = array();
if (preg_match($pattern, $string, $matches)) {
echo $matches['file'];
}
something along the lines
$style = "width: 40px; height: 30px; background-image: url('./files/foo/bar.png');";
preg_match("/url[\s]*\(([\'\"])([^\'\"]+)([\'\"])\)/", $style, $matches);
var_dump($matches[2]);
it wont work for filenames that contain ' or ". It basically matches anything between the parenthesis of url() that is not ' or "
Hi
I am writing a php code and and am supposed to display a large number of images on the webpage and hence I am using mysql data base to store the coordinates and taking them using SELECT and then trying to position them.
But I am not able to parse the cordinate as variable.
echo "img style=\"position :absolute; top:123px;left:123px\" border=\"0\" src=\"throbber.gif\" alt=\"Image\"/>";
in this place I want to do:
echo "img style=\"position :absolute; top:$variable1 px;left:variable2 px\" border=\"0\" src=\"throbber.gif\" alt=\"Image\"/>";
Please help me with this..
Try the following:
echo '<img style="position:absolute;top:'.$posX.'px;left'.$posY.':px;border:none;" src="throbber.gif" alt="Image" />
Note that you need to start the <img>-tag with a <. I moved the border-attribute to CSS (styles do not belong in markup) and used single quotes (') instead of double quotes ("), to prevent clashes inside the string (escaping sequences are rather ugly to read). Besides that, i used string concatenation (operator is .) to add the variables to the string.
Since there shouldn't be a space between the variable and the "px" you could use the concatenation operator "." for doing this.
It works like this:
$a = "hello";
$b = 10;
echo $a . $b; // Output: hello10
So
echo '<img style="position: absolute; top: ' . $variable1 . 'px; left: ' . $variable2 . 'px;" border="0" src="throbber.gif" alt="Image">';
should work.
You can use curly brackets around the variables:
echo "<img style=\"position: absolute; top: {$variable1}px; left: {$variable2}px\" border=\"0\" src=\"throbber.gif\" alt=\"Image\"/>";