Jump to content

Converting Perl code to PHP


BusyBeet

Recommended Posts

I'm trying to convert a perl script I wrote some time ago to PHP.  It checks the form submitted and uses some reg expressions to format the text of the textarea fields and display the formatted text.

Since I'm fairly new to PHP, I don't know the proper syntax for making an array of the content and spliting it by newline, then running through some elseif logic to check for types of content then substituting in a foreach loop.

For example, in Perl I had:

@lines = split(\n,$tableofcontents);

foreach (@lines, $wz) {

if ($wz =~  '[0-9]\.(.*?)\W' ) {

s|([0-9]\.(.*?)\W|<a href=\"\#H$1\"\>$1. $2\<\/a\>\<br\>\n|g;


etc, etc...

SO I tried rewriting it as...


$wordz == explode('\n',$toc);

foreach ($wordz as $wz) {

if ($wz =~  '[0-9]\.(.*?)\W' ) {




...It's erroring on the foreach line - what is the proper way to make and loop an array from one form field?

Thanks!

BB



Link to comment
Share on other sites

You'll want to use:
$wordz = explode("\n", $toc);

rather than $wordz == explode('\n',$toc);

== is the comparision operater, checks whether something on the left is equal to something on the right.
= is the assignment operator

Also notice I use double quotes around \n. This becuase PHP will treat \n as a string if use single quotes, rather than as a new line character.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.