Convert eregi_replace [closed] - preg-replace

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I migrated to a new PHP Version. Now I have lots of eregi_replace statements throwning errors:
$text = eregi_replace("([ \r\n])http://([^ ,\r\n]*)","\\1[url]http://\\2[/url]", $text);
$text = eregi_replace("([ \r\n])https://([^ ,\r\n]*)","\\1[url]https://\\2[/url]", $text);
$text = eregi_replace("([ \r\n])ftp://([^ ,\r\n]*)","\\1[url]ftp://\\2[/url]", $text);
$text = eregi_replace("([ \r\n])www.([^ ,\r\n]*)","\\1[url]http://www.\\2[/url]", $text);
$text = eregi_replace("^http://([^ ,\r\n]*)","[url]http://\\1[/url]", $text);
$text = eregi_replace("^https://([^ ,\r\n]*)","[url]https://\\1[/url]", $text);
$text = eregi_replace("^ftp://([^ ,\r\n]*)","[url]ftp://\\1[/url]", $text);
$text = eregi_replace("^www.([^ ,\r\n]*)","[url]http://www.\\1[/url]", $text);
I know I have to use preg_replace. As another person wrote this code I have no clue how to achive this. Maybe someone can help me out.

This would be the equivalent of the first eregi_replace you have
$text = preg_replace("#([ \r\n])http://([^ ,\r\n]*)#i","$1[url]http://$2[/url]", $text);
generalized that would mean:
$text = preg_replace('#'.$yourCurrentRegexString.'#i',str_replace('\\','$',$yourCurrentReplacement), $text);

Related

how to reverse a hash in perl without using built in function? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
1) %hash = (
1 => 'a',
4 => 'g',
7 => 'h'
);
WAP to reverse of the given hash without using reverse function?
You should really learn Perl - it is really fun when you learn it yourself.
It is really that simple:
my %rev;
foreach my $key (keys %hash) {
$rev{$hash{$key}} = $key;
}
TIMTOWTDI:
my %a = ( foo => 1, bar => 2 ); # original hash
my #a = %a; # copy to array
my #rev; # reversed list container
push #rev, pop #a while #a; # fill #rev from the back forward
my %rev = #rev; # assign reversed list to hash
my %by_letter;
#by_letter{ values(%by_num) } = keys(%by_num);
or
my %by_letter = map { $by_num{$_} => $_ } keys(%by_num);

Split Files into multiple files/arrays - perl [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to split a text file into multiple files based on content using perl sripting.
My file content is as below...
Header1:123
data...
1234...
SubTitle1:HHHHHHH
mmmm
aaaaa
SubTitle2:QQQQQQQ
mmmm
aaaaa
Header:MMMMMMMMMMM
datasadsad...
123434324...
SubTitle1:HHHHHHH
mmmm343343243
aaaaa dsfdfds
SubTitle2:QQQQQQQ
mmmm sff
aaaaasfsdf
And I want to split this file as below....
File 1 or array or anyobject to hold this....
Header1:123
data...
1234...
SubTitle1:HHHHHHH
mmmm
aaaaa
SubTitle2:QQQQQQQ
mmmm
aaaaa
File2 as below..
Header:MMMMMMMMMMM
datasadsad...
123434324...
SubTitle1:HHHHHHH
mmmm343343243
aaaaa dsfdfds
SubTitle2:QQQQQQQ
mmmm sff
aaaaasfsdf
Please help me on this!
my $fh;
while (<>) {
if (/^Header/) {
open($fh, '>', ...) or die $!;
}
print($fh $_);
}

Can i randomize parameters with Perl [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
is there a way with Perl to randomize all parameters for example :
http://s.example.com/x?list=hahaha&a=&b=&c=&1
so all parameters with &a=&b=&c=&1 etc.etc.etc.
this is the output i expect?
for (1..10){
#random this http://s.example.com/x?list=hahaha&a=&b=&c=&1
#to be like this
print "\n";
#http://s.example.com/x?list=hahaha&b=&a=&c=&1
#http://s.example.com/x?list=hahaha&c=&a=&b=&1
##http://s.example.com/x?list=hahaha&1=&b=&1=&c
}
So could anyone help with this??
Thnx allready
You provided not that much information, but here's a (very simple) approach that should get you started.
Code
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use URI;
use URI::QueryParam;
# prepare
my #all = map {chr $_} 32 .. 126;
my #word = grep /\w/ => #all;
# randomization helpers
sub rand_number { 1 + rand 10 }
sub rand_param_name { join '' => map {$word[rand #word]} 1 .. rand_number }
sub rand_param_value { join '' => map {$all[rand #all]} 1 .. rand_number }
# create three uris with randomized parameters
for (1 .. 3) {
# prepare
my $uri = URI->new('http://s.example.com/x');
# add randomized parameters
$uri->query_param(rand_param_name() => rand_param_value)
for 1 .. rand_number;
# done
say $uri;
}
Output
http://s.example.com/x?TC10=MX3+B_i!
http://s.example.com/x?BaXgf=%26%7BY%5Dxt%25o&l0f=HM%5Cd%24JB%3E)&lDQ7a94=Xh%3AB3N1jK_&dzGNz3=Y8NX4o9%5BS&YnCMxDdTew=J%5B%7B~b%23&Mr8UpMhPo=H&8_nAwh8ZHe=9%5DRY%40V&EgheAy='Uk&gmqTwKW=7PyitIn&3=V%5EKApdS%2C
http://s.example.com/x?dBA7WswJ5=GX-x&yKHeb=1y1sq%3EjPt&7G=7oa%24XF%7C)jQ&FjVo5IpF=%3BZ%5C&JB7OLer=%7C55&bYo64Fiy=L%23%40&ZhSohJVZ=-W+Rp5c+%5DD&aP=zJP%7Bu&pE5URXCWq=PHZJ%60%3D97L&6Xw7Da7=.Drb

Insert new line between two lines with perl [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to insert line beetwen line 2 and line 3 that contain concatenate string from this lines
abc
abcd:
abc
abcd
Output:
abc
abcd:
abcd: abcd
abc
abcd
You want to add something after a line that ends with a colon, or after line 2?
If after line 2, you can split("\n", $string) to get an array of lines, splice the new line into the array in position 2, and then join("\n", #array) to get the string back.
If after the line ending in the colon, you can use a regex: s/(:\n)/\1YOUR_NEW_LINE_HERE\n/.
Since you don't specify what you want to put after each line that ends with a colon, I've created a table to stand for some generic decision-making and somewhat flexible handling.
# create a table
my %insert_after
= ( abcd => "abcd: abcd\n"
, defg => "defg: hijk\n"
);
# create a list of keys longest first, and then lexicographic
my $regs
= '^('
. join( '|', sort { length $b <=> length $a or $a cmp $b }
keys %insert_after
)
. '):$'
;
my $regex = qr/$regs/;
# process lines.
while ( <> ) {
m/$regex/ and $_ .= $insert_after{ $1 } // '';
print;
}
"Inserting" a line after the current one is as easy as appending that text to the current one and outputting it.
perl -p -i.bck -e "if ($last ne ''){ $_=~s/.*/$last $&\\n$&/; $last=''} elsif (/:/) {$last = $_;chomp($last);} else {$last = '';}" test
test is the file in question

What do Perl's grep and map do? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
What does this code actually do?
#array = ( 'hai','hello','bar','foo' ) ;
print grep (/hai/ , #array );
print grep ("hai",#array ) ;
print map (/hai/ , #array );
print map ("hai",#array ) ;
It invokes the map and grep functions. A description of which can be found in the perlfunc entries for grep and map.
Is Google broken today?
Try it out...
print grep (/hai/ , #array );
hai
# writes all elements from #array containing 'hai' in them
print grep ("hai",#array ) ;
haihellobarfoo
# writes all elements, because "hai" evaluates to true
print map (/hai/ , #array );
1
# writes 1 for the only element from the #array, that contains 'hai'
print map ("hai",#array ) ;
haihaihaihai
# maps 'hai' to each element from #array