FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Find Strings (Tokens) in File and push them into an array - How?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Find Strings (Tokens) in File and push them into an array - How? [message #169537] Fri, 17 September 2010 12:09 Go to next message
DigitalDude is currently offline  DigitalDude
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
Hey,


I need to solve this problem and by now I'm really stuck...

I managed to go through the files I want to look for the tokens in a
loop and I checked them with str_pos and get a correct message that a
token is in my template.

My tokens look like this:

$__txtToken1; $__txtToken2; ...

I get the String-Position of the beginning of the token, but I just
can't figure out how I could parse my file and save these tokens to my
array!

Does anyone here have an idea how to accomplish this?

Regards...!
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169538 is a reply to message #169537] Fri, 17 September 2010 12:17 Go to previous messageGo to next message
DigitalDude is currently offline  DigitalDude
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
Just for the record: the parsing is not the problem, just how to get
the token from the file into my array. I need to copy each token that
matches a pattern (e.g. "$__txt" or so) and then save it as a string
into an array (which I would probably do with array_psuh or so).
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169539 is a reply to message #169538] Fri, 17 September 2010 12:22 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 17/09/2010 14:17, DigitalDude escribió/wrote:
> Just for the record: the parsing is not the problem, just how to get
> the token from the file into my array. I need to copy each token that
> matches a pattern (e.g. "$__txt" or so) and then save it as a string
> into an array (which I would probably do with array_psuh or so).

You can append stuff to arrays with this syntax:

$my_array[] = 'The new element';

http://es.php.net/manual/en/language.types.array.php



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169540 is a reply to message #169537] Fri, 17 September 2010 12:23 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 17/09/2010 14:09, DigitalDude escribió/wrote:
> I need to solve this problem and by now I'm really stuck...
>
> I managed to go through the files I want to look for the tokens in a
> loop and I checked them with str_pos and get a correct message that a
> token is in my template.
>
> My tokens look like this:
>
> $__txtToken1; $__txtToken2; ...
>
> I get the String-Position of the beginning of the token, but I just
> can't figure out how I could parse my file and save these tokens to my
> array!

If you know the length, simply use substr():

http://es.php.net/substr


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169541 is a reply to message #169538] Fri, 17 September 2010 12:31 Go to previous messageGo to next message
matt[1] is currently offline  matt[1]
Messages: 40
Registered: September 2010
Karma: 0
Member
On Sep 17, 8:17 am, DigitalDude <e.blumsten...@googlemail.com> wrote:
> Just for the record: the parsing is not the problem, just how to get
> the token from the file into my array. I need to copy each token that
> matches a pattern (e.g. "$__txt" or so) and then save it as a string
> into an array (which I would probably do with array_psuh or so).

Sounds like you need regex:

preg_match_all("/(\$__txt[^;]*);/", $fileTextString, $matches);
print_r($matches);

Not a complicated job to take structure of $matches and manipulate it
to your needs.

ref: http://us2.php.net/manual/en/book.pcre.php
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169544 is a reply to message #169541] Fri, 17 September 2010 13:04 Go to previous messageGo to next message
DigitalDude is currently offline  DigitalDude
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
On 17 Sep., 14:31, matt <matthew.leonha...@gmail.com> wrote:
> On Sep 17, 8:17 am, DigitalDude <e.blumsten...@googlemail.com> wrote:
>
>> Just for the record: the parsing is not the problem, just how to get
>> the token from the file into my array. I need to copy each token that
>> matches a pattern (e.g. "$__txt" or so) and then save it as a string
>> into an array (which I would probably do with array_psuh or so).
>
> Sounds like you need regex:
>
> preg_match_all("/(\$__txt[^;]*);/", $fileTextString, $matches);
> print_r($matches);
>
> Not a complicated job to take structure of $matches and manipulate it
> to your needs.
>
> ref:http://us2.php.net/manual/en/book.pcre.php


Hey,

yeah that seems to be the right approach, although I get a lot of
emtpy arrays this way. I hope I can modify this to a working version,
I cannot believe it is so complicated to setup such a trivial
function...
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169548 is a reply to message #169544] Fri, 17 September 2010 13:31 Go to previous messageGo to next message
DigitalDude is currently offline  DigitalDude
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
Hey,

ok I did it like this:

foreach($files as $file) {
$handle = file_get_contents($file);
preg_match_all('/(\$__txt[^;]*);/', $handle, $matches);
if(!empty($matches[0])) {
foreach($matches[0] as $token) {
array_push($tokens, $token);
}
}
}

The result Array looks like this:

Array
(
[0] => $__txtTokenInFrontend1;
[1] => $__txtTokenInFrontend2;
[2] => $__txtTokenInFrontend3;
)

which is exactly what I needed. So thanks guys, especially Matt for
the great reg-ex!
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169556 is a reply to message #169537] Fri, 17 September 2010 19:34 Go to previous messageGo to next message
aaaa is currently offline  aaaa
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
Regular expressions are slow.

$file = file_get_contents('aaaaaa.txt'); //or whatever you got
$temp = explode(';', $file);
foreach ($temp AS $i=>$t)
{
$t = trim($t, ' ;__$');
if ($t == '') unset($temp[$i]);
}

//your array:
print_r($temp);
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169560 is a reply to message #169556] Fri, 17 September 2010 20:49 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(aaaa)

> Regular expressions are slow.

Not necessarily. And in most cases their flexibility and powerfulness
far outweigh their performance issue.

Micha
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169566 is a reply to message #169560] Fri, 17 September 2010 21:59 Go to previous messageGo to next message
aaaa is currently offline  aaaa
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
>> Regular expressions are slow.
>
> Not necessarily. And in most cases their flexibility and powerfulness
> far outweigh their performance issue.

In most cases their debugging takes more time.
Plus their are plain evil.
It's like programmers use functions, objects, coding standards and comments
that make their code in many cases longer, but easier to understand and
read. Regular expressions are exacly the opposite- hard to read, understand
and debug. Using regular expressions is like writting obscure code.

--
A
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169568 is a reply to message #169566] Fri, 17 September 2010 23:50 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 9/17/2010 5:59 PM, aaaa wrote:
>>> Regular expressions are slow.
>>
>> Not necessarily. And in most cases their flexibility and powerfulness
>> far outweigh their performance issue.
>
> In most cases their debugging takes more time.
> Plus their are plain evil.
> It's like programmers use functions, objects, coding standards and comments
> that make their code in many cases longer, but easier to understand and
> read. Regular expressions are exacly the opposite- hard to read, understand
> and debug. Using regular expressions is like writting obscure code.
>

Regular expressions are not that bad once you understand them. Just
because YOU don't like them doesn't mean they aren't worthwhile. They
are very valuable in many instances.

And the equivalent using non-regex code can be MUCH more complicated.

My suggestion: learn how to use regex's correctly. But I already know
you won't, because you've already made up your mind.

And no, I'm not good at them. But I struggle and still use them,
because they are the best tool for many of the things I run into.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169574 is a reply to message #169566] Sat, 18 September 2010 09:17 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(aaaa)

>>> Regular expressions are slow.
>>
>> Not necessarily. And in most cases their flexibility and powerfulness
>> far outweigh their performance issue.
>
> In most cases their debugging takes more time.
> Plus their are plain evil.
> It's like programmers use functions, objects, coding standards and comments
> that make their code in many cases longer, but easier to understand and
> read. Regular expressions are exacly the opposite- hard to read, understand
> and debug. Using regular expressions is like writting obscure code.

Obviously you haven't really understood what they are and when pattern
matching is the most appropriate tool. Of course you can completely
avoid them, but there are enough cases where a replacement for a single-
line regex would be many dozen lines of string manipulation functions.
_That_ would be hard to debug and understand.

Yes, regexps are much easier to write than to read, but that doesn't
make them less useful. You just need a good understanding of how they
work internally.

Micha
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169580 is a reply to message #169574] Sat, 18 September 2010 11:34 Go to previous messageGo to next message
aaaa is currently offline  aaaa
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
> Obviously you haven't really understood what they are and when pattern
> matching is the most appropriate tool. Of course you can completely
> avoid them, but there are enough cases where a replacement for a single-
> line regex would be many dozen lines of string manipulation functions.
> _That_ would be hard to debug and understand.
>
> Yes, regexps are much easier to write than to read, but that doesn't
> make them less useful. You just need a good understanding of how they
> work internally.

I do understand how they work and *sometimes* I use them.
But it doesn't change the fact they are obscure/obfuscated.
And having 10 lines of fast, idiot understandable code is much better then
having one line of regular expressions.

--
A
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169585 is a reply to message #169580] Sat, 18 September 2010 13:03 Go to previous messageGo to next message
rf is currently offline  rf
Messages: 19
Registered: September 2010
Karma: 0
Junior Member
"aaaa" <aaaa(at)aaaaaaaaaaaaaaaaaaa(dot)com> wrote in message
news:4c94a3ce$0$20987$65785112(at)news(dot)neostrada(dot)pl...
>> Obviously you haven't really understood what they are and when pattern
>> matching is the most appropriate tool. Of course you can completely
>> avoid them, but there are enough cases where a replacement for a single-
>> line regex would be many dozen lines of string manipulation functions.
>> _That_ would be hard to debug and understand.
>>
>> Yes, regexps are much easier to write than to read, but that doesn't
>> make them less useful. You just need a good understanding of how they
>> work internally.
>
> I do understand how they work and *sometimes* I use them.
> But it doesn't change the fact they are obscure/obfuscated.
> And having 10 lines of fast, idiot understandable code is much better then
> having one line of regular expressions.

Head. sand. Stuck in.
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169588 is a reply to message #169580] Sat, 18 September 2010 15:01 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(aaaa)

>> Obviously you haven't really understood what they are and when pattern
>> matching is the most appropriate tool. Of course you can completely
>> avoid them, but there are enough cases where a replacement for a single-
>> line regex would be many dozen lines of string manipulation functions.
>> _That_ would be hard to debug and understand.
>>
>> Yes, regexps are much easier to write than to read, but that doesn't
>> make them less useful. You just need a good understanding of how they
>> work internally.
>
> I do understand how they work and *sometimes* I use them.
> But it doesn't change the fact they are obscure/obfuscated.
> And having 10 lines of fast, idiot understandable code is much better then
> having one line of regular expressions.

That's just your opinion, not a fact.

Micha
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169592 is a reply to message #169588] Sat, 18 September 2010 17:08 Go to previous messageGo to next message
Sherm Pendley is currently offline  Sherm Pendley
Messages: 33
Registered: September 2010
Karma: 0
Member
Michael Fesser <netizen(at)gmx(dot)de> writes:

> .oO(aaaa)
>
>> And having 10 lines of fast, idiot understandable code is much better then
>> having one line of regular expressions.
>
> That's just your opinion, not a fact.

True, but it's an opinion that's shared by a *lot* of people. When it
comes to maintenance, ten lines of readable code is *always* better
than a "clever" one-liner.

sherm--

--
Sherm Pendley
<http://camelbones.sourceforge.net>
Cocoa Developer
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169593 is a reply to message #169592] Sat, 18 September 2010 17:42 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 9/18/2010 1:08 PM, Sherm Pendley wrote:
> Michael Fesser<netizen(at)gmx(dot)de> writes:
>
>> .oO(aaaa)
>>
>>> And having 10 lines of fast, idiot understandable code is much better then
>>> having one line of regular expressions.
>>
>> That's just your opinion, not a fact.
>
> True, but it's an opinion that's shared by a *lot* of people. When it
> comes to maintenance, ten lines of readable code is *always* better
> than a "clever" one-liner.
>
> sherm--
>

Regex's are not necessarily a "clever one-liner". Rather, if you
understand them, they clarify code immensely.

Sure, a complicated regex will be hard to understand. However, it also
won't be possible to repeat it's effects in just 10 lines of simple code.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169606 is a reply to message #169592] Sat, 18 September 2010 21:15 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(Sherm Pendley)

> Michael Fesser <netizen(at)gmx(dot)de> writes:
>
>> .oO(aaaa)
>>
>>> And having 10 lines of fast, idiot understandable code is much better then
>>> having one line of regular expressions.
>>
>> That's just your opinion, not a fact.
>
> True, but it's an opinion that's shared by a *lot* of people. When it
> comes to maintenance, ten lines of readable code is *always* better
> than a "clever" one-liner.

Yes, but only _if_ it can be done in ten lines. When I use regular
expressions, I usually have a lot of optional and "dynamic" stuff in it:
arbitrary patterns, variable pattern lengths, special character classes,
alternative patterns and all these things. Doing that with just string
functions would require a lot of if-else constructs, switch statements
and a lot of additional checks.

If I then want to modify the behaviour by adding some other special case
for example, I might have to write a whole new code block instead of
just adding some chars to the regex pattern. Not to mention the fun you
might have when you want to recreate the behaviour of some of the PCRE
modifiers. For example doing things in a case-insensitive way might
require some whole checks to be written twice. In a regex it's just one
char.

And if a regex gets too complex, it makes sense to break it down into
pieces and comment it. Especially the last feature can make things much
easier to understand, but is rarely used.

A short note about the example posted by 'aaaa': I don't think his code
with all the exploding and trimming is so much clearer or better than
the posted regex. Sure you get the same result, but for me the regex
version is better because it's more written as intended and pretty much
self-explaining. You have some data, and you want to search for some
special pieces in it. The regex version does exactly that with a single
function call.

The code could even be shortened a bit. My version would be:

foreach ($files as $file) {
$text = file_get_contents($file);
preg_match_all('/\$__txt.+?;/', $text, $matches);
$tokens = array_merge($tokens, $matches[0]);
}

The function names already make completely clear what's going on.

The other version returns the same result, but works in a completely
different way, and from just looking at it it's IMHO not immediately
clear what it does. I have to walk through it and "execute" it in my
mind to understand what's going on.

Micha
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169618 is a reply to message #169606] Sun, 19 September 2010 10:30 Go to previous messageGo to next message
aaaa is currently offline  aaaa
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
> The code could even be shortened a bit. My version would be:
>
> foreach ($files as $file) {
> $text = file_get_contents($file);
> preg_match_all('/\$__txt.+?;/', $text, $matches);
> $tokens = array_merge($tokens, $matches[0]);
> }

foreach ($files as $file) {
$text = file_get_contents($file);
$matches = explode(' ', $text);
$tokens = array_merge($tokens, $matches);
}

Same result, but easier (and shorter!) code. And I can even make it shorter:

foreach ($files as $file) {
$text = file_get_contents($file);
$tokens = array_merge($tokens, explode(' ', $text));
}

Or even shorter:

foreach ($files as $file) {
$tokens = array_merge($tokens, explode(' ', file_get_contents($file)));
}

And it's still clearer code then your reg.exp.

--
A
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169619 is a reply to message #169618] Sun, 19 September 2010 13:20 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 9/19/2010 6:30 AM, aaaa wrote:
>> The code could even be shortened a bit. My version would be:

<bunch of code snipped>

And how does that solve the op's problem? The answer is, it doesn't.
So it's worthless.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169620 is a reply to message #169618] Sun, 19 September 2010 14:21 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(aaaa)

>> The code could even be shortened a bit. My version would be:
>>
>> foreach ($files as $file) {
>> $text = file_get_contents($file);
>> preg_match_all('/\$__txt.+?;/', $text, $matches);
>> $tokens = array_merge($tokens, $matches[0]);
>> }
>
> foreach ($files as $file) {
> $text = file_get_contents($file);
> $matches = explode(' ', $text);
> $tokens = array_merge($tokens, $matches);
> }
>
> Same result, but easier (and shorter!) code. And I can even make it shorter:
> […]

Indeed, in this case. If the OP's files are really just a bunch of
tokens without any other content and he always wants the entire string,
then just splitting the entire file would be enough. But usually the
situation is at least slightly more complicated, for example when things
like '$__txt' and ';' are used as delimiters and what matters are the
strings between them.

Micha
Re: Find Strings (Tokens) in File and push them into an array - How? [message #169621 is a reply to message #169580] Sun, 19 September 2010 15:55 Go to previous message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
On Sat, 18 Sep 2010 13:34:52 +0200, aaaa wrote:
>> Obviously you haven't really understood what they are and when pattern
>> matching is the most appropriate tool. Of course you can completely
>> avoid them, but there are enough cases where a replacement for a single-
>> line regex would be many dozen lines of string manipulation functions.
>> _That_ would be hard to debug and understand.
>>
>> Yes, regexps are much easier to write than to read, but that doesn't
>> make them less useful. You just need a good understanding of how they
>> work internally.
>
> I do understand how they work and *sometimes* I use them.
> But it doesn't change the fact they are obscure/obfuscated.

They're *dense*. They're obscure because you don't know what they're
doing. They're not *at all* obfuscated becuase that means they've been
INTENTIONALLY made more obscure than necessary. But nobody ever bothers
to do that in practice because that makes them LESS dense, and slower.

> And having 10 lines of fast, idiot understandable code is much better then
> having one line of regular expressions.

The "fast" part is the hard bit. Lots of VERY smart people have worked
really hard to make regexp parsing and searching as efficient as it can
be, and shared those methods across most (if not all) implementations of
regexp.

--
38. If an enemy I have just killed has a younger sibling or offspring
anywhere, I will find them and have them killed immediately,
instead of waiting for them to grow up harboring feelings of
vengeance towards me in my old age. --Anspach's Evil Overlord list
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Advanced SQL injection complete tool pack(tutorials,scanners,admin finder,shells,md5)
Next Topic: Stats comp.lang.php (last 7 days)
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 16:40:54 GMT 2024

Total time taken to generate the page: 0.03192 seconds