This question already has answers here:
How do I strip all spaces out of a string in PHP? [duplicate]
(4 answers)
Closed 6 years ago.
I would like to remove white space in a php generated css code, I have different title header which would like to generate through php code.
Titles are like 1) Baby Day Care 2) Baby Night Care
My php codes are like:
<div class="wistia<?php echo $subject->title;?>">
So when I got the class, I got like wistiaBaby Day Care or wistiaBaby Night Care
But I want the class will be look like wistiaBabyDayCare or wistiaBabyNightCare
(I want space removed between text).
So how I am going to achieve that?
That should to the trick:
<div class="wistia<?php echo str_replace(' ', '', $subject->title);?>">
This just uses the str_replace function where you replace the first character with the 2nd character for a given string.
To remove any whitespace simply use preg_replace (regex):
<div class="wistia<?php echo preg_replace('/\s+/', '', $subject->title);?>">
Related
This question already has answers here:
Break up/parse a URL into its constituent parts in php
(1 answer)
How to remove the querystring and get only the URL?
(16 answers)
Closed 3 years ago.
I have a series of strings created as URLs as the page gets refreshed.I want to remove a portion from the right side of these strings. The strings look like:
http://funfun.ca/?image=1
http://funfun.ca/?image=14
http://funfun.ca/?image=217
and so on. The goal is for anything starting with "?" to be removed from the right side of the strings and leave the following for all of them:
http://funfun.ca/
I appreciate any help I can have to solve this
You may explode the url string for ? and save only the first part obtained:
$firstPart = explode('?', $url)[0];
Example:
echo $firstPart = explode('?', 'http://funfun.ca/?image=1')[0];
returns:
http://funfun.ca/
This question already has answers here:
How do I remove ASCII number 13?
(1 answer)
PHP remove line break or CR LF with no success
(9 answers)
How to remove ANSI-Code ("
") from string in PHP
(4 answers)
Closed 4 years ago.
Ok, maybe is simple question but i am generating a xml from custom posts of wordpress, the problem is a textarea field of acf, i put a break line here and the code generate give me this character
but i not find nothing about that in google and str_replace not make nothing about that. i already remove the <br /> and <br> but that character I can't.
this is my actual code about that
str_replace('
', '', str_replace('<br />', '', strip_tags($addressValue['c_restaurant_map_address'], '<br /><br/><br>')));
This question already has answers here:
How do I write a regex in PHP to remove special characters?
(3 answers)
Closed 9 years ago.
I would like to replace all characters in a string other than [a-zA-Z0-9\-] with a space.
I've found this post and no matter how many times I tweak the REGEX, I can't get it to "don't match [a-zA-Z0-9-], only match and replace other characters".
At the moment, I have the following:
$original_name = trim(' How to get file creation & modification date/times in Python? ');
$replace_strange_characters = preg_replace("/^((?!([a-zA-Z0-9\-]+)*))$/", " ", $original_name);
// Returns: How to get file creation & modification date/times in Python?
echo $replace_strange_characters;
I wish for it to replace all of the strange characters with a space ' ', thus returning:
How to get file creation modification date times in Python?
I'm really struggling with these "don't match" scenarios.
Here's my code so far: http://tehplayground.com/#2NFzGbG7B
You may try this (You mentioned you want to keep dash)
$original_name = trim(' How to get file creation & modification date/times in Python? ');
$replace_strange_characters = preg_replace('/[^\da-z-]/i', ' ', $original_name);
echo $replace_strange_characters;
DEMO.
Wouldn't this regex do it?
[^a-zA-Z0-9\-]
Oh, and why are you doing this btw?
Try this
$replace_strange_characters = preg_replace("([^a-zA-Z0-9\-\?])", " ", $original_name);
The previous answer strip out the ?
This question already has answers here:
How to remove text between tags in php?
(6 answers)
Strip HTML tags and its contents
(2 answers)
Closed 9 years ago.
I want to remove part of string between two html tags. I have something like this:
$variable = "This is something that I don't want to delete<blockquote>This is I want to delete </blockquote>";
the problem is that the string between blockquote tag is changing, and its need to be deleted, no matter what it is. Anyone now how?
Regex are not the best thing to parse html string, you should take a look at Simple HTML dom parser or the php DOMDocument class.
If you still want to use a regex in this case it will be for example :
$variable = preg_replace('/<blockquote>.+<\/blockquote>/siU', '', $variable);
Test it there.
You can use regular expressions, but this is by no means fail-safe and should only be used in trivial cases. A better way is to use a full-fledged HTML parser.
<?php
$str = preg_replace('#<blockquote>.*</blockquote>#siU', '', $str);
?>
please try using regex in this way
<?php
$variable = "This is something that I don't want to delete<blockquote>This is I want to delete </blockquote>";
$str = preg_replace('#(<blockquote>).*?(</blockquote>)#', '$1$2', $variable);
print($str);
?>
This question already has answers here:
Regular Expression for extracting text from an RTF string
(11 answers)
Closed 9 years ago.
A column in the database I work with contains RTF strings, I would like to strip these out using PHP, leaving just the sentence between.
It is a MS SQL database 2005 if I recall correctly.
An example of the kind of strings pulled from the database (need any more let me know, all the rest are similar):
{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fnil\fcharset0 Tahoma;}}
\viewkind4\uc1\pard\lang1033\f0\fs17 ASSEMBLE COMPONENTS AS DETAILED ON DRAWING.\lang2057\fs17\par
}
I would like this to be stripped to only return:
ASSEMBLE COMPONENTS AS DETAILED ON DRAWING.
Now, I have successfully managed to strip the characters in ASP.NET for a previous project, however I would like to do so using PHP. Here is the regular expression I used in ASP.NET, which works flawlessly may I add:
"(\{.*\})|}|(\\\S+)"
However when I try to use the same expression in PHP with a preg_replace it does not strip half of the characters.
Any regex gurus out there?
Use this code. it will work fine.
$string = preg_replace("/(\{.*\})|}|(\\\S+)/", "", $string);
Note that I added a '/' in the beginning and at the end '/' in the regex.