Php string concatenating variables with forward slash [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying to create a unique id by concatenating 3 variables using forward slashes(/). my code is like so
$year . "/" . $acronym . "/" . $num;
I am expecting an output of
"18/MC/1"
but the output I get is
"18\/MC\/1"
What am I doing wrong. I have already tried using stripslashes() but it doesn't do anything to the output.

You could use join function
<?php
// ...
join([$year, $acronym, $num], '/');
For more info, see: Join function documentation

I found what was wrong,
The code below outputs the correct format 18/MC/1
return response()->json([$id]);
while my previous code was
return response()->json($id);
which gave me an output of
`"18/MC/1"

There is function in php to remove backward slash from string use following function.
echo stripslashes(string);
output: 18/MC/1

Related

PHP for : IF URL = string else [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I've made progress here , updating this support request .. i cannot add code to this it won't save - this is very difficult , spent 5 minutes sending this simple message.
ltrim removes prefix "/" AND ..
preg-replace remove suffix "?page=*"
so that :
URL = /city = city
URL = /city?page=* = city
You can't use <?=...?> inside a string, that can only be used when you want to print something from a context that's printing literal text.
Either use variable interpolation in a double-quoted string:
if($host == "https://www.purelocal.com.au/{$profession[filename]}")
or concatenation:
if($host == 'https://www.purelocal.com.au/' . $profession[filename])

How can i write " in php [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
function wrap($str) {
$str="[#id=\"".$str."\"]";
return($str);
}
$str="Hi";
$str=wrap($str);
I would have $str like [#id="Hi"], but i have $str like [#id=\"Hi\"]
How could i do that?
$str='[#id="'.$str.'"]';
replace " with '
The code works as expected, no need to change quotes to single ticks or remove the second pair of double quotes.
Probably the backslashes are added later. If you just echo $str; after your snippet it shows this in a browser
[#id="Hi"]

find and replace special characters in PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Please tell me how to do a find and replace in PHP.
I nee to find Entry="OK" and replace with OK in a xml file.
I tried str_replace but confused because of the = and " in it.
Even though I'm not sure of the environment you're working in, here's a php sample that I think does what you want:
<html>
<?php
$teststr = "Entry=\"OK\"";
print $teststr . "<br>";
$newanswer = str_replace($teststr, "Entry=OK", $teststr);
print $newanswer . "<br>";
?>
</html>
Try this way
str_replace('Entry="OK"','OK',$var);
You can use str_replace its working
$r = 'Entry="OK" tyyuu hhh';
echo str_replace($r,'Entry="OK"','OK');
if any error found then u use preg_replace with corresponding patteren

Add text directly next to PHP variable? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a variable inside an inline style -
style='color:$custom_color;font-size:$custom_icon_size;'
And am trying to add the letters px directly after the $custom_icon_size. This obviously does not work? Any ideas what is the correct method?
thanks
you need to use {} around your PHP vaariable
style='color:$custom_color;font-size:${custom_icon_size}px;'
Assuming that you're echoing it, you have two options:
echo "style='color:$custom_color;font-size:{$custom_icon_size}px;'";
echo "style='color:" . $custom_color . ";font-size:" . $custom_icon_size . "px;'";
$style = 'color:'.$custom_color.'; font-size: '.$custom_icon_size.'px';
echo 'style="'.$style.'"';

How to check a string contain alphabet or not [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a simple php program which is getting the data from the url, but I want to check whether it contains a alphabet or not.
The data which I get from the url is as follows ?query=seller,12. I used ctype_alpha() for this, but I am getting no result for this. I have a rough idea that it can be done by preg_match() but I don't how to do it.
Please help me as I am beginner to php.
Thanks in advance
Here you go,
<?php
$subject = "?query=seller,12";
if(preg_match('/[a-zA-Z]/', $subject)){
echo "It has a alphabet";
}
?>
if you want to print all the characters from that string, you can use like this
preg_match_all('/[a-zA-Z]/', $subject,$matches);
print_r($matches);
$matches is an array of all available matches of the specified pattern
Try this dude.
if (!preg_match('/[^A-Za-z]/', $string)) // '/[^a-z\d]/i' should also work.
{
// string contains only english letters
}

Categories