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 ";

View 4 Replies


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

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

RegEx Remove Space After Every Comma In String?

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

Remove Extra Space

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

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

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

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

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

Add An Extra Space After Period In A String

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

Regex - Regular Expression To Remove Number Then A Space?

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

Regex - Remove Space Additionally To The Special Chars?

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

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

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

Regex Html Plus With Extra String At The End?

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

Remove Space From String?

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

Remove A 'space' From The Middle Of A String

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

Remove The White Space Within The String?

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

View 1 Replies

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

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

Function To Remove The Space Inside The String?

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

Regex Replace Space In String?

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

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

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

Regex To Match White Space Or End Of String

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

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

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

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

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

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

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

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

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

Extra Space Is Added

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

Extra Space From If Statements

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

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

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

Copyrights 2005-15, All rights reserved