Remove Extra Space From A String Using Regex?

Sep 21, 2010

How do I remove extra spaces at the end of a string using regex (preg_replace)?

$string = "some random text with extra spaces at the end ";


Preg Replace - Remove Extra Space At The End Of String Using Preg_replace?

Jan 24, 2011

I want to replace the extra space at the end of the string with nothing using preg_replace in php. i was creating a big database of words and somehow few words got extra white space at the end.

View 3 Replies View Related

RegEx Remove Space After Every Comma In String?

Feb 4, 2011

I have a string of names separated by commas. I'm exploding this string of names into an array of names with the comma as the delimiter. I need a RegEx to remove a white space(if any) only after the comma and not the white space between the first and last name.

So as an example:

$nameStr = "Sponge Bob,Bart Simpson, Ralph Kramden,Uncle Scrooge,Mickey Mouse";

See the space before Ralph Kramden? I need to have that space removed and not the space between the names. And I need to have any other spaces before names that would occur removed as well.

P.S.: I've noticed an interesting behavior regarding white space and this situation. Take the following example:

When not line breaking an echo like so:

$nameStr = "Sponge Bob,Bart Simpson, Ralph Kramden,Uncle Scrooge,Mickey Mouse";
$nameArray = explode(",", $nameStr);
foreach($nameArray as $value)
echo $value;


View 6 Replies View Related

Remove Extra Space

Sep 22, 2007

Is there a way to take a string that the user has submitted and check the beginning for any extra spaces, and then remove the extra space up to the first real "character" (letter or number)? I hope that isn't too confusing, just ask if you need me to explain better.

View 3 Replies View Related

Extra Space Before And After Text Link!! Need To Remove?

Nov 7, 2010

I have a text link generated in PHP for bread crumbs, and I can't figure out how to eliminate the extra space before and after the link that is generated. This is the LAST LINK:

Website link: link text
<div id="crumbs">

View 2 Replies View Related

Remove Extra Spaces / Tabs And Line Feeds From A Sentence And Substitute Them With Just One Space?

Apr 4, 2011

remove multiple whitespaces in php

I have a stream of characters, a sentence or a paragraph, which may have extra spaces in two words or even tabs or line feeds, how can I remove all these and substitute them by a single whitespace.

View 1 Replies View Related

Add An Extra Space After Period In A String

Jul 17, 2005

I want to be able to add an extra space in each instance
after a period in a string. I am already stripping out extra
whitespace throughout, so to start I have a nice flat string to work
with. I am struggling getting the syntax right and keep getting funny
results. What is the simplest way to accomplish this?

View 9 Replies View Related

Regex - Regular Expression To Remove Number Then A Space?

Sep 6, 2010

How would I go about removing numbers and a space from the start of a string?

For example, from '13 Adam Court, Cannock' remove '13 '

View 5 Replies View Related

Regex - Remove Space Additionally To The Special Chars?

Mar 2, 2010

in my functions.php if have this code:

echo '<a href="' . preg_replace('/s/', '-', $search) . '-keyword1.html">' . urldecode ($search) . '</a>';

this removes the special chars..

but how can i additonally add remove space and replace it with - and remove "

so, if someone types in "yo! here" i want yo-here

View 4 Replies View Related

Remove Extra Characters At The End And Beginning Of A String?

Aug 9, 2010

How can I get rid of extra hyphens at the end and beginning of user submitted text for example. I want visual-studio-2008 instead of -visual-studio-2008- is there a way I can remove the extra hyphens using PHP?

View 3 Replies View Related

Regex Html Plus With Extra String At The End?

Mar 17, 2011

I am trying to find HTML tags, via PHP ereg, that has a {xxx} right before the closing tag - where xxx can be [A-Za-z0-9_-]*.For example: <p>xxxx</p><p>yyyy{asdf}</p>This is the best I could come up with: /<([w]+)([^>]?)>([^{]*){([^}]+)}</1>/isThe problem is though, that it will match group 3 will have xxxx</p><p>yyyy, and I only want yyyy in this case

View 1 Replies View Related

Remove Space From String?

Sep 20, 2005

Is there php function to remove the space inside the string? for example: $abcd="this is a test" I want to get the string: $abcd="thisisatest"

View 9 Replies View Related

Remove A 'space' From The Middle Of A String

Jul 16, 2005

I have been using PHP for a while now, although my knowledge of the inbuilt functions is pretty lacking. I am trying to remove a 'space' from the middle of a string.

I reckon I could split the string at teh space, trim each string then join them, but this seems a little long winded. Is there a command to remove certain charachters? Or even better white space? I've searched the PHP manual with little luck.

View 3 Replies View Related

Remove The White Space Within The String?

May 17, 2011

So if my string looks like this "2011 -01-01" how can I remove the white space within the string?

View 1 Replies View Related

Remove All Non-alphanumeric And Non-space Characters From A String?

Mar 23, 2010

I want to remove all non-alphanumeric and space characters from a string. So I do want spaces to remain. What do I put for a space in the below function within the [ ] brackets:

ereg_replace("[^A-Za-z0-9]", "", $title);

In other words, what symbol represents space, I know represents a new line, is there any such symbol for a single space.

View 2 Replies View Related

Function To Remove The Space Inside The String?

Apr 19, 2010

Is there php function to remove the space inside the string? for example: $abcd="this is a test" I want to get the string: $abcd="thisisatest"

View 2 Replies View Related

Regex Replace Space In String?

Jun 14, 2011

I'm messing about with the LESS PHP parser to get it to replace 4 colour hex codes found in IE filters. What I want to do is replace stuff like this: #ff7755 33 with #ff775533 ie. remove all the spaces in it.Obviously the characters can vary as they're colour codes. I found this question which is very close to what I want.Right now, I have this regex which finds the string just fine:


All I need now is the regex to put in the replace argument of preg_replace().

View 2 Replies View Related

Put A Space In Front Of Capitals In A String (Regex)?

Jul 6, 2009

I have a number of strings which contain words which are bunched together and I need to seperate them up.

For example
ThisWasCool - This Was Cool
MyHomeIsHere - My Home Is Here

Im slowly getting my head around regular expressions and I believe to do this I should use preg_replace. My problem is putting together the expression to find the match.

I have only got this far

preg_replace('~^[A-Z]~', " ", $string)

Each string contains a lot of words, but ONLY the first word contains bunched words so using my example above a string would be "ThisWasCool to visit you again" - "This Was Cool to visit you again"

I have told it to start at the beginning, and look for capitals, but what I dont know how to do is

- restrict it only to the first word of each string

- how to reuse the capital letter in the replace part after the space

View 4 Replies View Related

Regex To Match White Space Or End Of String

Mar 22, 2010

I'm trying to find every instance of @username in comment text and replace it with a link. Here's my PHP so far:

$comment = preg_replace('/@(.+?)s/', '<a href="/users/${1}/">@${1}</a> ', $comment);

The only problem is the regex is dependent upon there being whitespace after the @username reference.

View 2 Replies View Related

Splitting String On Spaces But Allow A Space In 1 Field Using Regex?

Apr 20, 2011

This is kind of a follow on from this post: Regex for splitting params out using preg_matchI have this string 1 0 61 12345678 sierra007^7 0 0 26429 25000 and I need to get each element. It was suggested I use explode which was a great simple solution but now I need to allow spaces in one of the fields.Someone else posted this regex:


That does everything else and I was wondering if it could be modified to allow spaces in field 5 (sierra007^7). The only advice I can offer is that the rest of the fields are always numeric (or a colon as you can see) before and after field 5. Is this possible with 1 regex statement or do I need to parse it in PHP and fudge it together?EDIT: For example, field 5 could be sierra007^7 OR si erra007^7 or si er ra007^7. It would know that it came across field 5 as its the only one that contains a-zA-Z characters. It would know where field 5 ends because field 6 only contains 0-9 characters.

View 2 Replies View Related

Regex - Split A String By Space - Numbers And Quotes?

Dec 2, 2010

I am parsing a string in PHP that has the following pattern

VARIABLE Key1 Value1 Key2 Value2 Key3 Value3 ...
similar to:
JOBGRADE 'P' 'Parttime Employee' 'C' 'Customer Support'
SOMEVARIABLE 1 "Value1" 2 'Value2'

This line starts with an unquoted string and can have single or double quoted strings and/or numbers. It can have one to multiple key value pairs. I need to split the string in 2 ways: The first to get the unquoted string that is not numeric. The second to extract the numeric value and/or quoted strings - can be single or dobule

Thus I need
P:Parttime Employee
C:Customer SupportOR

My Thoughts: I thought about splitting the string and iterating through it to test: for 1: If value is not numeric and not quoted it is the variable name for 2+: Not sure the easy way to do this because I must detect the difference between the keys and values:

View 2 Replies View Related

Validate Of User Data - Trim (remove White Space Before And After A String) And Numbers

Jan 24, 2011

I have a simple form and a script to do some validating of user data, among other tasks. One part of the script is intended to verify a birthday.

Code: [Select]$x = $_POST['x'];
$x = trim($x);

According to the manual, trim() is supposed to remove whitespace before and after a string. My intent by using trim() is to determine if someone tried to skip the birthday field. I have other validation running on the value of $x, but I wanted trim() to remove whitespace.

However, the value of x, which in this case is expected to be numeric, such as 12 for a month, ends up being 1. This happens with any 2-digit number that a user might enter into the form field for x. The second number is always removed. Any insight into why trim() removes the second number, 2 in the above example, leaving only a "1" as the value of $x

View 1 Replies View Related

Regex Match Multiple String Combinations Inside A String And Remove Unwanted Letters Between All Matches

Nov 17, 2010

I have a string with several lines of CSS code. I want to match all occurences of :

background:url(" url ");

and remove everything except the filename. Example input:


View 1 Replies View Related

Extra Space Is Added

Sep 10, 2006

I used the search option but i couldnt get solution for my problem. When i retrieve value from mysql db and place in a textbox an extra space is attached at the end.

View 8 Replies View Related

Extra Space From If Statements

Jul 27, 2005

For some odd reason, the more fields that I enter information for (that are NOT required) adds more empty, white space to the top of the php page.

It has something to do with all my if statements in the php. I viewed the source and there are not any extra breaks or anything to place the information something like 10 lines down. Code:

View 5 Replies View Related

Regex - Remove Numbers Like [ 3 ] [ 2 ] [ 4 ] From String?

Jul 10, 2010

I have a text string that contain some references numbers like [3][2][4] or [1] or [5][7]

removing all numbers between brackets ex. [4] from that string

using regular expressions Regex ?

View 2 Replies View Related

Regex Remove Bracket In String?

Jun 14, 2010

similiar like this example, [URL]i have no idea to replace

$str = '(ABC)some text'
$str = 'ABC';

currently use $str = preg_replace('/(.)/','',$str); but not works. how to fix this?

View 4 Replies View Related

Remove Part Of A String With Regex?

Apr 19, 2011

I'm trying to strip part of a string (which happens to be a url) with Regex. I'm getting better out regex but can't figure out how to tell it that content before or after the string is optional. Here is what I have

$new_string= preg_replace('/[/?refid=0-9]+/', '', $string);
echo $new_string;

I'm trying to remove the ?refid=22 part to get [URL]

EDIT I think I need to use Regex instead of explode becuase sometimes the url looks like [URL] In this case I also want to remove the refid but not get id=9999

View 2 Replies View Related

Regex - Remove Numbers From A String?

Apr 26, 2011

$text = preg_replace('/[^\/-a-zs]/i', '', $text);

I have this expression, which removes everything except dashes, forward slashes and whitespace. However, it always leaves the numbers no matter what I move around.

$text = "FLAMBOYANT#_45/ADJ" = result = FLAMBOYANT45/ADJ

View 4 Replies View Related

Regex - Remove Everything In The String - Last Digits?

Feb 4, 2011

I have values like below:

$var1 = car-123-244343
$var2 = boat-2-1
$var3 = plane-311-23

I need to remove everything and keep the last digit/ditgits after the second hyphen

Expecting values:

This is what I've got
$stripped = preg_replace('^[a-z]+[-]$', '', 'car-123-244343');

I got a big red error No ending delimiter '^' found

View 5 Replies View Related

Regex To Remove The Digits From A String?

Jun 18, 2011

I need a regex to remove the digits from a string but not also the spaces. I currently have

$city_location = 'UK,0113|Leeds new york';
$sip_city = '0113Leeds new york';
$city = preg_replace('/[^a-z]/i', '', $sip_city);

It removes the digits but also the spaces so I need a regex which won't remove the spaces.

View 2 Replies View Related

Regex To Remove Year From A String

May 20, 2011

I have a database full of cigarette and trade cards, each set title has a year associated with it e.g. 1943 or 2011 etc always 4 chars but can be anywhere in the string. What would be a good regex to extract the year from the string? I tried '/d{4}/' but it failing for some reason

View 3 Replies View Related

Getting Extra Line Space In Mail...

Oct 27, 2005

I have a txt file with a canned message. When it is sent and arrives at my mailbox it has doubled line breaks... Code:

View 10 Replies View Related

Extra Space In Text Area?

Mar 12, 2009

I have some text that get's displayed in a textarea but for some reason there's a load of spaces at the start so my message get's displayed like below.

how text is getting displayed

I've tried adding a text-align: left; to the textarea but it still doesn't fix it

View 3 Replies View Related

Does Extra Space Slow Down The Processor

Sep 15, 2010

how to "correctly" unset a node, I noticed that using PHP's unset() function leaves the tabs and spaces behind. So now I have this big chunk of white space in between nodes at times. if PHP iterates through blank spaces/returns/tabs and whether it would eventually slow down the system.


This is how I removed the whitespaces after unsetting a node and it worked for me.

$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;

View 4 Replies View Related

Regex - Remove The Probable Dot At The Beginning/end Of String?

Jul 1, 2010

I tried this:

echo preg_replace('/[^,,$]/', '', ',test,hi,');

But gets:


View 3 Replies View Related

Regex String Remove Everything But Center Numbers?

Mar 9, 2011

I have a string "Mod. 816-10 025" and I want to remove everything but the 816-10.Conditions:The number in the center will always be ###-##It may not be 3 numbers - 2 numbers but it will always be space numbers dash numbers spac

View 6 Replies View Related

Regex - Remove All Break Tags From A String ?

Jul 6, 2010

Seems simple enough but I'm having some issues, I tried using preg_replace:

preg_replace("<br />", "", $string);

But what I get is this <> in place of the <br /> in the string when outputted. The break tags in the string will always be in this format not in any of these:

<BR />

so what's the error I'm making?

View 2 Replies View Related

Regex - Remove All Non-matching Characters In String?

Oct 3, 2010

I've got text from which I want to remove all characters that ARE NOT the following.

desired_characters =


The last is a (newline) that I do want to keep.

View 2 Replies View Related

Regex - Remove Brackets/contents From A String?

Aug 26, 2009

If I have a string like this:

$str = "blah blah lah (a) (b) blah blah blah";

How can I regex so that the output is:

$str = "blah blah blah blah blah blah";

Needs to be able to support any number of bracket pairs inside a string.

View 3 Replies View Related

Regex - Function To Remove Key From A Query String?

Dec 21, 2010

I have a query string, like:



I just want a php function that will accept the query string and the name (key), and remove the name value pair from the querystring.

Every example I have found uses regexes and assumes that the value will exist and that it will not be empty, but in my case (and possibly in general, I would like to know) this would also be a valid query:


I dont know in advance how many keys there will be.

I am convinced that regexes are monsters that eat time. Eventually all matter in the universe will end up in a regex.

View 3 Replies View Related

Regex - Remove Control Characters From String?

Sep 30, 2009

How to remove control characters like STX from a php string.preg_replace("/[^a-zA-Z0-9 .-_;!:?<>='"]/","",$pString) but found that it removed way to much.Is there a way to remove only control chars?

View 4 Replies View Related

Find Extra Space / New Line After A Closing ?

May 25, 2011

So I have a space/new line after a closing ?> (php tag) that is breaking my application.How can I find it easily I have 1000 of files and 100000 lines of code in this app.Ideally im after some regex combined with find grep to run on a unix box.

View 6 Replies View Related

Extra Space Being Added In Array Output

Jun 5, 2011

I'm trying to create a custom function to take a array and add comma. Like for a location or list of items.


San Francisco, California, United States is the output. It should out put San Francisco, California, United States

View 3 Replies View Related

Getting Extra White Space On HTML Output?

Apr 30, 2010

I'm getting extra white space, that is not CSS, or nothing like it on the view output: The HTML.

I've checked for ?> (removed, where I could), saved UTF8 without BOM. Checked for existent white space in the beginning of each file, even at end.

This is the structure:

index.php - this is the entry point;

Let's say, that trough method GET, its sent the var TPL with some value. Let's call it LIST, so it pulls the LIST model, with all data and then show the right template to the user, with the right data.

I used and tested, with require_once, include_once, include, even tested with readfile (just to test). The LIST Template opens the header.tpl and footer.tpl; I also tryed to remove this both includes from LIST template, but still, the extra white space continued.

This is where the extra white space is coming from. This controller is placed between <body> controller activity runs here </body>, this is where the extra white space is coming from:

$rows = $model_works->rows;
if ( !require_once('views/list_works.tpl.php') ) {
echo "<p>Error.</p>";
} // end if clause

The list_works.tpl.php, is basicly HTML with tags; I've t tested by changing the extension to something else, like html.

Also, just to remember that at top of this file, we are using require_once to open the header.tpl and at bottom the footer.tpl. I've tested by removing both and the extra white space was still generated.

The extra white space is being generated here:

<!-- some stuff working fine -->

View 1 Replies View Related