I have large text files that I read into an array, but before that I take out all the special characters such as tabs, new lines, and returns. However I'm left with all the extra spaces (sometimes as many as 20 or more at a time) because my users are allowed to type spaces in their text. For storage and other manipulation, I don't need all those spaces.
If I explode it I'm left with thousands of empty (null?) elements in the array.
I can take the spaces out before exploding by doing: - $spacesCount= substr_count($ztagsdata," ");
-
$spacesCount=$spacesCount/2;
-
echo $spacesCount;
-
for ($i=1;$i<$spacesCount;$i++)
-
{
-
$ztagsdata= str_replace(" ", " ", $ztagsdata);
-
}
which replaces double spaces with single spaces through a loop--which is a bit overkill (but it works), because i can't just replace double spaces with "" because then there's no space between each legitimate word)
any suggestions how to get out the extra spaces (and still leave a single space)? Or should I go ahead and explode into an array and then delete the null (empty?) values--which I don't know how to do. I know pop and push but can't find how to delete a middle element....
any suggestions are welcomed.
7 11215
Why don't you just use a simple regular expression to remove all extra blank spaces from your string? Like this one:[php]$string = preg_replace('/\s\s+/', ' ', $string);[/php]
Ronald :cool:
Good heavens! that was so easy.
It worked like a charm.
(I'll take another look at pearl regular expressions to see exactly how that works).
Thank you so much Ronald!
Thank you.
my typo: s/b perl not pearl
- $mystring = preg_replace('/\s\s+/', ' ', $mystring);
fyi to anyone who cares.
whitespace is represented by: \s
a single space can be represented by: ' '
so, a loose translation of the above code would be:
replace any space (\s) that is followed by multiple spaces (\s+) with a single space (' ') in the string named $mystring, and put the results back into $mystring.
Thanks again Ronald!!
and....
I think this also means that if you have an array with null or empty elements you can implode it with a space separator ie: -
$mystring= implode(' ', $an_array);
then use the above code to wipe out any extra spaces,
then explode it back into an array - $myarray=explode(' ', $mystring)
and the empty or null elements will be gone, right?
A null is not identical to a blank char. A null is the absence of a value, like void. It has no data type and no value. A blank is a real character, it has a data type CHAR and a value blank.
Ronald :cool:
Great, got it!
Thanks for the heads up on that one.
:)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: deko |
last post by:
I'm kind of lost on this one - I need to modify 2 files based on user input:
$data_array = file($data_file);
$counter_array = file($counter_file);
// There is a line-for-line relationship between the data and counter files
//for example, if the 3rd line in the counter file is deleted,
//so also must the 3rd line of the data file be deleted.
// Each line of the data_file has 4 items:
//date|ip_address|user_id|url
for ( $i=0; $i <...
|
by: peter |
last post by:
Hello,
for a C++ delete:
class Test{};
Test * A = new Test;
delete A;
We don't use delete A;
How can C++ compiler can tell there are 50 destructor to deallocate?
|
by: Roy Smith |
last post by:
I understand that "delete xp" deletes a scalar object and "delete xp"
deletes an array of objects, but what I don't understand is why you need
to tell the compiler which you're doing.
When you do "delete xp", the delete procedure (not sure if that's the
right terminology) obviously knows how many objects were allocated by
the corresponding "new" call. So, why can't it just know whether you
did "new x" or "new x"?
|
by: Brian Underhill via DotNetMonster.com |
last post by:
I am trying to delete an element from an array. I have an array of 52
elements and I want to search for an element and delete it. Therefore,
making it an array of 51 elements.
is it just
delete MyArray;
any help would be great....thanks
|
by: |
last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily
complicated than the other languages I know. I feel this is because there
aren't many good official resources out there to help do the most basic
things.
One of the "basic" things I haven't been able to find out how to do is how
to delete an item from a multidimensional array object and resize it
afterwards.
It seems so easy to conceive of the code to delete...
| |
by: flash |
last post by:
write a program that manipulates arrays of integers. The main program should call three functions: Insert, Delete, and Search. The Insert function should call a function Sort that sorts the array.
Here is a description of the three functions:
Insert: Accepts as input the array and the element you want to insert into the array. The function should insert the element at the end of the array and should call the function Sort to sort the...
|
by: Jon Slaughter |
last post by:
Is it safe to remove elements from an array that foreach is working on?
(normally this is not the case but not sure in php) If so is there an
efficient way to handle it? (I could add the indexes to a temp array and
delete afterwards if necessary but since I'm actually working in a nested
situation this could get a little messy. I guess I could set there values to
null and remove them afterwards?
Thanks,
Jon
|
by: ITAutobot25 |
last post by:
My delete button is not working in my GUI and my due date is today before midnight. Can anyone show me how to correct this error? My assignment statement is below as well as 5 classes. InventoryGUI is the main class to begin execution.
Thanks!
• Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform the corresponding actions on the item name, the...
|
by: subramanian100in |
last post by:
Suppose
class Base
{
public:
virtual ~Test() { ... }
// ...
};
class Derived : public Base
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |