I had a lot of trouble searching google for this and it never returned what I was looking for. It seems simple enough but I can't see how its done.
$string = "any";
$text = "$stringway";
echo $text;
output: anyway
Of course it doesn't work because it is looking for a string called $stringway when I actually want it to output the string called $string with the plain text "way" concatenated on the end. I therefore need some method to separate the string from the text without actually adding a space.
I'm sure there's an incredibly simple solution.
Simply enclose your variable $string within {}(braces) or you can use .(dot) concatenation operator
$string = "any";
$text = "{$string}way";
echo $text;
The reason to use {} braces is because when you write "$stringway" it will search for $stringway which leads to undefined variable and with braces it'll consider it as $string."way"
You mean this?
$string = "any";
$text = $string."way";
echo $text;
Use the string concatenation operator . to combine strings.
Related
I have an alphanumeric string like 1234and5678.
I want to store the numbers preceding and i.e 1234 into one variable and the number after and i.e 5678 into another variable as shown below:
$var1=1234;
$var2=5678;
also what should do if i replace and by some random special characters like #$% etc.
Can you please help me out?
Thanks in advance.
$string = "1234and5678";
list($before, $after) = explode("and", $string);
This splits the string into two variables based on the delimeter ("and"), whatever is before and is saved in a variable called $before, whatever is after is saved into a variable called $after
Use split http://php.net/manual/en/function.split.php with "and" as separator. That should give you an array with the two numbers.
you can use preg_split() function, in your case: $var = preg_split("/[\D]+/", "1234and5678"); That gives you $var[0] = '1234' and $var[1] = '5678'
According to you edit, you can:
replace regex on any another that you need (e.g. $var = preg_split("/[\d]+/", "1234and5678"); for any non-numeric element)
use preg_match_all("/(\d+)/","1234and5678", $var) to find any numbers in your string
Use regular expressions. I provided a link for regexes in php. Split is deprecated as of version 5.3.0 and you should not rely on it.
Perl compatible regular expressions
I've some text that i wish to parse
$str = "text1<br/>text2<br/>text3
I've tried using
print_r( preg_split("<br/>", $str));
but it is not giving me the desired output
Try the following:
$str = "text1<br/>text2<br/>text3";
print_r(preg_split("/<br\/>/", $str));
I'm assuming missing the closing quote " at the end of the $str = "text1<br/>text2<br/>text3" is just a typo.
Take a look at this page on how to specify the string $pattern parameter: http://php.net/manual/en/function.preg-split.php
It's because you're not using the correct regular expression. Is there a reason you can't use explode()? Regex is problematic, overly complicated at times, and much slower. If you know you'll always be splitting at the BR tag, explode is much more efficient.
Parsing HTML with regex is a bad idea, but here you go:
var_dump(preg_split('/(<br\ ?\/?>)+/', $str));
I've been looking all over the internet for some useful information and I think I found too much. I'm trying to understand regular expressions but don't get it.
Lets for instance say $data="A bunch of text [link=123] another bunch of text.", and it should get replaced with "< a href=\"123.html\">123< /a>".
I've been trying around a lot with code similar to this:
$find = "/[link=[([0-9])]/";
$replace = "< a href=\"$1\">$1< /a>";
echo preg_replace ($find, $replace, $data);
but the output is always the same as the original $data.
I think I have to see something relevent to my problem understand the basics.
Remove the extra [] around the (), and add + after the [0-9] to quantify it. Also, escape the [] that make up the tag itself.
$find = "/\[link=(\d+)\]/"; // "\d" is equivalent to "[0-9]"
$replace = "$1";
echo preg_replace($find,$replace,$data);
The regex would be \[link=([\d]+)\]
A good source for an quick overview of regular expression can you find here http://www.regular-expressions.info/
When you really interested in the power of regular expression, you should buy this book: Mastering Regular Expressions
A good Programm to test your RexEx on a Windows Client is: RegEx-Trainer
You are missing the + quantifier and as a result of this your pattern matches if there is a single digit following link=.
And there is an extra pair of [..] as a result of this the outer [...] will be treated as the character class.
You also forgot the escape the closing ].
Solution:
$find = "/[link=([0-9]+)\]/";
<?php
$data= "A bunch of text [link=123] another bunch of text.";
$find = '/\[link=([0-9]+?)\]/';
echo preg_replace($find, "$1", $data);
In C you can continue a string literal in the next line escaping the newline character:
char* p = "hello \
new line.";
( My C is a bit rusty and this could be non 100% accurate )
But in php, the backslash is taking literally:
$p = "hello \
new line.";
I.E. the backslash character forms part of the string.
Is there a way to get the C behavior in PHP in this case?
Is it possible to simply concatenate your string like this:
$p = "hello " .
"new line.";
There's a few ways to do this in PHP that are similar, but no way to do it with a continuation terminator.
For starters, you can continue your string on the next line without using any particular character. The following is valid and legal in PHP.
$foo = 'hello there two line
string';
$foo = 'hello there two line
string';
The second example should one of the drawbacks of this approach. Unless you left jusity the remaining lines, you're adding additional whitespace to your string.
The second approach is to use string concatenation
$foo = 'hell there two line'.
'string';
$foo = 'hell there two line'.
'string';
Both example above will result in the string being created the same, in other words there's no additional whitespace. The trade-off here is you need to perform a string concatenation, which isn't free (although with PHP's mutable strings and modern hardware, you can get away with a lot of concatenation before you start noticing performance hits)
Finally there's the HEREDOC format. Similar to the first option, HEREDOC will allow you to break your strings over multiple lines as well.
$foo = <<<TEST
I can go to town between the the start and end TEST modifiers.
Wooooo Hoooo. You can also drop $php_vars anywhere you'd like.
Oh yeah!
TEST;
You get the same problems of leading whitespace as you would with the first example, but some people find HEREDOC more readable.
In PHP you can simply continue on a new line with a string.
eg.
<?php
$var = 'this is
some text
in a var';
?>
The short answer is that you cannot do it as easily as in C, you need to either concatenate (best):
$p = "This is a long".
" non-multiline string";
or remove the newlines afterwards (awful, don't do this):
$p = "This will contain the newlines
before this line";
//for instance str_replace() can remove the newlines
OK,I know that I should use a DOM parser, but this is to stub out some code that's a proof of concept for a later feature, so I want to quickly get some functionality on a limited set of test code.
I'm trying to strip the width and height attributes of chunks HTML, in other words, replace
width="number" height="number"
with a blank string.
The function I'm trying to write looks like this at the moment:
function remove_img_dimensions($string,$iphone) {
$pattern = "width=\"[0-9]*\"";
$string = preg_replace($pattern, "", $string);
$pattern = "height=\"[0-9]*\"";
$string = preg_replace($pattern, "", $string);
return $string;
}
But that doesn't work.
How do I make that work?
PHP is unique among the major languages in that, although regexes are specified in the form of string literals like in Python, Java and C#, you also have to use regex delimiters like in Perl, JavaScript and Ruby.
Be aware, too, that you can use single-quotes instead of double-quotes to reduce the need to escape characters like double-quotes and backslashes. It's a good habit to get into, because the escaping rules for double-quoted strings can be surprising.
Finally, you can combine your two replacements into one by means of a simple alternation:
$pattern = '/(width|height)="[0-9]*"/i';
Your pattern needs the start/end pattern character. Like this:
$pattern = "/height=\"[0-9]*\"/";
$string = preg_replace($pattern, "", $string);
"/" is the usual character, but most characters would work ("|pattern|","#pattern#",whatever).
I think you're missing the parentheses (which can be //, || or various other pairs of characters) that need to surround a regular expression in the string. Try changing your $pattern assignments to this form:
$pattern = "/width=\"[0-9]*\"/";
...if you want to be able to do a case-insensitive comparison, add an 'i' at the end of the string, thus:
$pattern = "/width=\"[0-9]*\"/i";
Hope this helps!
David