Need Example Using PHP

Discussions and Tech Support related to automating the iMacros Browser or Internet Explorer from any scripting and programming language, such as VBS (WSH), VBA, VB, Perl, Delphi, C# or C++.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information:CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
LSnelson
Posts: 17
Joined: Thu Jan 29, 2009 6:02 pm

Need Example Using PHP

Post by LSnelson » Tue Sep 15, 2009 5:55 pm

I've been using VBScript for a while and want to migrate to PHP. In VB you can build your macro inline like this:

MyMacroCode = "CODE:"
MyMacroCode = MyMacroCode+"VERSION BUILD=5200814" + vbNewLine
MyMacroCode = MyMacroCode+"TAB T=1" + vbNewLine
MyMacroCode = MyMacroCode+"TAB CLOSEALLOTHERS" + vbNewLine
MyMacroCode = MyMacroCode+"URL GOTO=http://www.iopus.com" + vbNewLine
MyMacroCode = MyMacroCode+"URL GOTO=http://forum.iopus.com"


Does anyone have an example of creating the macro using PHP and constructing the macro inline as above?

Thanks much,
Larry
Hannes, Tech Support

Re: Need Example Using PHP

Post by Hannes, Tech Support » Wed Sep 16, 2009 8:27 am

Code: Select all

        $str = 'CODE:';
        $str = $str . 'line1' . "\r\n";
        $str = $str . 'line2' . "\r\n";
or

Code: Select all

        $str = 'CODE:';
        $str .= 'line1' . "\r\n";
        $str .= 'line2' . "\r\n";
Cf. http://www.php.net/manual/en/language.types.string.php
Last edited by Hannes, Tech Support on Wed Sep 16, 2009 8:32 am, edited 1 time in total.
Reason: replaced "\n" by "\r\n"
LSnelson
Posts: 17
Joined: Thu Jan 29, 2009 6:02 pm

Re: Need Example Using PHP

Post by LSnelson » Thu Sep 17, 2009 1:06 am

Thank you. That's exactly what I wanted.
ABooth
Posts: 223
Joined: Mon Aug 10, 2009 4:25 pm

Re: Need Example Using PHP

Post by ABooth » Fri Sep 18, 2009 1:15 am

You should always use string builders rather than repeated string concatenation, because concatenation is memory and processor intensive. Strings appear mutable, but in reality, string concatenation requires creating a new string, copying from the old string, then destroying the old string.

What you are doing with something like

Code: Select all

Dim myVar
myVar = "Some text"
myVar = myVar + " Extra Text"
myVar = myVar + " And Some More Text"

Response.Write ( myVar )
Is really this.
Line 1:-
Declare a pointer called myVar

Line 2:-
Reserve a 9 byte memory block and insert the Ascii values that represent the characters "Some text"
Point myVar to this memory block (assigning the value to the variable)

Line 3: -
Reserve a 20 byte block of memory
Copy the contents of the block of memory pointed to by myVar into the first 9 bytes of the 20 byte block
Reserve an 11 byte block of memory and put " Extra Text" in it
Copy the values in this memory to the 20 byte block, at the 10th Byte position onwards. (This is how the strings are concatenated)
Point myVar to this new 20 byte memory block containing "Some text Extra Text"
Release the memory blocks containing "Some text" and " Extra Text".

Line 4: -
Reserve a 39 Byte memory block
Copy the contents of the block of memory pointed to by myVar into the first 20 bytes of the 39 byte block
Reserve a 19 byte block of memory and put " And Some More Text" in it
Copy the values in this memory to the 39 byte block, at the 21st Byte position onwards. (Concatenating to the result of the previous concatenation)
Point myVar to this new 39 byte memory block containing "Some text Extra Text And Some More Text"
Release the memory blocks containing "Some text Extra Text" and " And Some More Text"

So with 1 assignment and 2 concatenations, you created & destroyed several memory blocks and copied values from memory block to memory block ("Some text" got assigned once and copied twice). All very memory and processor intensive. As you can also see, with every line of concatenation, more memory is reserved and more bytes are copied from block to block, to the point that just adding 1 more line can take up the equivalent processing and memory of all the previous lines combined, as you are creating buffers that contain all of the previous lines and the text to append, then copy them to an output memory block, the size of the 2 memory blocks you are concatenating. The memory usage and processing required, grow exponentially with the addition of each line of concatenation.

With a string builder, "Some text" doesn't get copied from memory block to memory block. It stays in the stringbuilder and isn't touched unless you've explicitly decided to remove it. Also It only gets copied once, when you output the stringbuilder to a string. It doesn't necessarily save the memory allocation, but it doesn't waste the memory and time it takes to create and destroy memory blocks. It only uses 1 memory block for each allocation. What it does is keep all the memory blocks, instead of the concatenation above. Finally, when you decide to access it as a whole string, it will copy the array of memory blocks to 1 big memory block once, creating a new string block and still leaving the array of memory blocks untouched unless you specifically request they be destroyed, or destroy the stringbuilder instance.


An example (With a 3rd party VBScript StringBuilder Class): -

Code: Select all

Dim myBuilder
Set myBuilder = New StringBuilder

myBuilder.Add "Some text"
myBuilder.Add " Extra Text"
myBuilder.Add " And Some More Text"

Response.Write ( myBuilder.ToString() )
The code is not difficult to understand as it has the same logical approach as the simple concatenation. You are adding extra string snippets to the object The performance, however will be significantly better than simple concatenation, especially with large amounts of concatenation. The StringBuilder uses a total of 39 bytes of memory to store the string snippets (it also has pointers to each array element and whatever else is in the StringBuilder object instance).

The simple concatenation created and destroyed memory blocks totaling 98 bytes for what resulted in a 39 byte string.
The StringBuilder used 39 Bytes for storage and created a 39 Byte output string (78 Bytes), with no overhead of copying block to block to block

Of course, the overhead of the StringBuilder object itself will have more than the 39 bytes used to store all the string snippets, because the StringBuilder object requires memory itself to exist, but the StringBuilder object can be reused and even if there was no memory saving (because it's used once to concatenate a few strings), there's always the performance improvement of copying memory blocks to the final output string, only once, instead of each concatenation.

It's Win - Win because it can save memory consumption, performs quicker, can manipulate the strings in different ways by having more methods than just ToString(), can combine the parts to create a different string, in combination with another StringBuilder instance. The string parts can be reused, sorted, manipulated without being part of 1 big string. Each myBuilder.Add line could contain just 1 word and you could sort them, search them or use the same string snippet twice in generating the output string, making memory use even more efficient. In other words, you gain flexibility as well as performance.

Example: -

Code: Select all

Dim myBuilder
Set myBuilder = New StringBuilder

myBuilder.Add "the"	 'Line 1
myBuilder.Add "quick"  'Line 2 
myBuilder.Add "brown"  'Line 3
myBuilder.Add "fox"    'Line 4
myBuilder.Add "jumps"  'Line 5
myBuilder.Add "over"   'Line 6
myBuilder.Add "lazy"   'Line 7
myBuilder.Add "dogs"   'Line 8

'Just Create a String
Response.Write ( myBuilder.ToString() )
'Output: thequickbrownfoxjumpsoverlazydogs

'Format it (Sentence case "the" to "The", separate each word with a space and terminate with a period ".")
Response.Write ( myBuilder.ToSentence() )
'Output: The quick brown fox jumps over lazy dogs.

'Change the order of the words, reuse words (the) and format
Response.Write ( myBuilder.ToSentenceOrder(1, 7, 4, 5, 6, 1, 2, 3, 8) )
'Output: The lazy fox jumps over the quick brown dogs.

'Generate output other than a plain string
Response.Write ( myBuilder.ToCsv() )
'Output: "the","quick","brown","fox","jumps","over","lazy","dogs"

'Output to a file
Response.Write ( myBuilder.ToSentenceFile("MyFile.txt") )
'Output: Text file MyFile.txt containing: The quick brown fox jumps over lazy dogs.

'Sort ascending and separate with spaces
Response.Write ( myBuilder.ToAscendingString() )
'Output: brown dogs fox jumps lazy over quick the
A little in depth, I know, but I hope that might prevent you from thinking repeated string concatenation is, in any way, a good solution and in case you forget why, you can re-read this and code in a better way.
iMacros for Firefox supports JavaScript Macros (Scripting)
JavaScript supports Java via LiveConnect

Therefore: You can write powerful macros with iMacros for Firefox. Have a look at this one

Post feature requests here. Maybe one day, they'll pin it?
Hannes, Tech Support

Re: Need Example Using PHP

Post by Hannes, Tech Support » Fri Sep 18, 2009 7:56 am

Impressing.
Thank you very much for that post!
Post Reply