Using explode but after first <br><br> Pattern - laravel

I am using explode to create an array from string and using pattern <br><br> but in my case i want it to be start after first match <br><br>. Means when first time it gets <br><br> then it skip and create an array from second time and so on match <br><br>
<?php
$myString = "Welcome, j.<br><br>
 
(1) this revisional application has been preferred under read with section of the against the order passed by the learned special judge, ndps act, 6th court at barasat.<br><br>
(2) case of the petitioner is that he along with other accused persons are facing trial case, as pending the learned additional sessions judge, 6th court. barasat.";
$myArray = explode('<br><br>', $myString);
// $arr = ltrim($myArray, ' ');
echo "<pre>"; print_r($myArray);
foreach($myArray as $key => $value)
{
$whatIWant = substr($value, strpos($value," "));
echo ucfirst($whatIWant);
}
Basically my task is to Capitalize first letter of Second word i.e this and case.

You can use array_shift() function to shift your array by one element and skip first item in your array.
php document for array_shift
$myArray = explode('<br><br>', $myString);
array_shift($myArray);
foreach($myArray as $key => $value)
{
$whatIWant = substr($value, strpos($value," "));
echo ucfirst($whatIWant);
}

I would try using preg_match_all instead.
The code below looks for the pattern (\d+) (\w+)
\d+ = 1 or more digits
\w+ = 1 or more alphanumeric letters
If you were to add (3)... (4)... and so on, it would match all of them
<?php
$myString = "Welcome, j.<br><br>
(1) this revisional application has been preferred under read with section of the against the order passed by the learned special judge, ndps act, 6th court at barasat.<br><br>
(2) case of the petitioner is that he along with other accused persons are facing trial case, as pending the learned additional sessions judge, 6th court. barasat.";
preg_match_all("/(\(\d+\))\s(\w+)/", $myString, $all_matches);
foreach ($all_matches[1] as $idx => $match) {
$original = "{$match} " . $all_matches[2][$idx];
$replacement = "{$match} " . ucfirst($all_matches[2][$idx]);
$myString = str_replace($original, $replacement, $myString);
}
echo $myString;

Related

Perl: how to combine consecutive page numbers?

OS: Windows server 2012, so I don't have access to Unix utils
Activestate Perl 5.16. Sorry I cannot upgrade the OS or Perl, I'm stuck with it.
I did a google search and read about 10 pages from that, I find similar problems but not what I'm looking for.
I then did 3 searches here and found similar issues with SQL, R, XSLT, but not what I'm looking for.
I actually am not sure where to start so I don't even have code yet.
I'd like to combine consecutive page numbers into a page range. Input will be a series of numbers in an array.
Input as an array of numbers: my #a=(1,2,5)
Output as a string: 1-2, 5
Input ex: (1,2,3,5,7)
Output ex: 1-3, 5, 7
Input ex: (100,101,102,103,115,120,121)
Output ex: 100-103,115,120-121
Thank you for your help!
This is the only code I have so far.
sub procpages_old
# $aref = array ref to list of page numbers.
# $model = used for debugging.
# $zpos = used for debugging only.
{my($aref,$model,$zpos)=#_;
my $procname=(caller(0))[3];
my #arr=#$aref; # Array of page numbers.
my #newarr=();
my $i=0;
my $np1=0; # Page 1 of possible range.
my $np2=0; # Page 2 of possible range.
my $p1=0; # Page number to test.
my $p2=0;
my $newpos=0;
while ($i<$#arr)
{
$np1=$arr[$i];
$np2=getdata($arr[$i+1],'');
$p1=$np1;
$p2=$np2;
while ($p2==($p1+1)) # Consecutive page numbers?
{
$i++;
$p1=$a[$i];
$p2=getdata($a[$i+1],'');
}
$newarr[$newpos]=$np1.'-'.$p2;
$newpos++;
# End of loop
$i++;
}
my $pages=join(', ',#arr);
return $pages;
}
That's called an intspan. Use Set::IntSpan::Fast::XS.
use Set::IntSpan::Fast::XS qw();
my $s = Set::IntSpan::Fast::XS->new;
$s->add(100,101,102,103,115,120,121);
$s->as_string; # 100-103,115,120-121
This seems to do what you want.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
while (<DATA>) {
chomp;
say rangify(split /,/);
}
sub rangify {
my #nums = #_;
my #range;
for (0 .. $#nums) {
if ($_ == 0 or $nums[$_] != $nums[$_ - 1] + 1) {
push #range, [ $nums[$_] ];
} else {
push #{$range[-1]}, $nums[$_];
}
}
for (#range) {
if (#$_ == 1) {
$_ = $_->[0];
} else {
$_ = "$_->[0]-$_->[-1]";
}
}
return join ',', #range;
}
__DATA__
1,2,5
1,2,3,5,7
100,101,102,103,115,120,121
The rangify() function builds an array of arrays. It traverses your input list and if a number is just one more than the previous number then it adds the new number to the second-level array that's currently at the end of the first-level array. If the new number is not sequential, it adds a new second-level array at the end of the first level array.
Having built this data structure, we walk the first-level array, looking at each of the second-level arrays. If the second level array contains only one element then we know it's not a range, so we overwrite the value with the single number from the array. If it contains more than one element, then it's a range and we overwrite the value with the first and last elements separated with a hyphen.
So I managed to adjust this code to work for me. Pass your array of numbers into procpages() which will then call num2range().
######################################################################
# In:
# Out:
sub num2range
{
local $_ = join ',' => #_;
s/(?<!\d)(\d+)(?:,((??{$++1}))(?!\d))+/$1-$+/g;
tr/-,/, /;
return $_;
}
######################################################################
# Concatenate consecutive page numbers in array.
# In: array like (1,2,5,7,99,100,101)
# Out: string like "1-2, 6, 7, 99-101"
sub procpages
{my($aref,$model,$zpos)=#_;
my $procname=(caller(0))[3];
my #arr=#$aref;
my $pages=num2range(#arr);
$pages=~s/\,/\-/g; # Change comma to dash.
$pages=~s/ /\, /g; # Change space to comma and space.
#$pages=~s/\,/\, /g;
return $pages;
}
You probably have the best solution already with the Set::IntSpan::Fast::XS module, but assuming you want to take the opportunity to learn perl here's another perl-ish way to do it.
use strict;
use warnings;
my #nums = (1,2,5);
my $prev = -999; # assuming you only use positive values, this will work
my #out = ();
for my $num (#nums) {
# if we are continuing a sequence, add a hyphen unless we did last time
if ($num == $prev + 1) {
push (#out, '-') unless (#out and $out[-1] eq '-');
}
else {
# if we are breaking a sequence (#out ends in '-'), add the previous number first
if (#out and $out[-1] eq '-') {
push(#out, $prev);
}
# then add the current number
push (#out, $num);
}
# track the previous number
$prev = $num;
}
# add the final number if necessary to close the sequence
push(#out, $prev) if (#out and $out[-1] eq '-');
# join all values with comma
my $pages = join(',', #out);
# flatten the ',-,' sequence to a single '-'
$pages =~ s/,-,/-/g;
print "$pages\n";
This is not super elegant or short, but is very simple to understand and debug.

how to find substring between given characters in smarty?

I have following code in php to which I am looking in smarty
function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
$fullstring = '[1]wholesale';
$parsed = get_string_between($fullstring, "[", "]");
$filename = substr(strrchr($fullstring, "]"), 1);
echo $parsed.'/'.$filename;
//Output 1/Wholesale
I want this output 1/Wholesalein two different variables (as done in php $parsed,$filename) in Smarty (Let say string is [1]wholesale).
You could achieve it this way:
{assign var="var" value="[1]wholesale"}
{$var|substr:($var|strpos:'['+1):($var|strpos:']' - $var|strpos:'['-1)}/{$var|substr:($var|strpos:']'+1)}
Output for this will be:
1/wholesale
However as you see it's rather quite illegible. You could of course move it to some function/modifier but as always I need to tell, the best option is to prepare data in PHP and then simply display it in Smarty not using advanced transformations.

Allow a-z, A-Z, 0-9, space and -. preg_match

How can I allow a-z, A-Z, 0-9, - and space with preg_match.
I currently have the following:
if (!preg_match("/^[_a-zA-Z0-9]+$/", $name))
Also a side-question, does any of you know a good guide to learn preg_match.
I think this will work for you
<?php
$result = preg_match("/^[a-zA-Z0-9 \s]+$/", $name));
?>
just give space and "-" in your brackets [ ]
example
<?php
$name = "A- ";
if(preg_match("/^[_a-zA-Z0-9- ]+$/", $name))
{
echo "hello";
}
?>
and a good site for studying : link
only [a-z, A-Z, 0-9 and Space] allow. We delete the first space with LTRIM.
<?php
$name = "Hasan Yİlmaz SÄ°MFER ";
if(ltrim(preg_match('/^[a-zA-ZığüşöçİGÜŞÖÇ0-9- ]+$/', $name2), ' '))
{
echo $name;
}
?>
Using /^[a-zA-Z'- ]+$/ Shows the below given warning
"preg_match(): Compilation failed: range out of order in character
class at offset 10"
Instead of the above-mentioned code, you can use /^[a-zA-Z' -]+$/. This will not show the warning.!

Use wordlist for Codeigniter captcha

I have a wordlist of dictionary words in .txt format. How can I use this with the captcha_helper instead of random characters? I've already extended the captcha_helper file but am having issues integrating my wordlist.txt file for use.
After doing some poking, I found a solution:
// This is the modified version in captcha_helper.php
if($word == ''){
$wordsfile = '../words.php';
$fp = fopen($wordsfile, 'r');
$length = strlen(fgets($fp));
$line = rand(1, (filesize($wordsfile)/$length)-2);
if(fseek($fp, $length*$line) == -1) return FALSE;
$word = trim(fgets($fp));
fclose($fp);
}
But I noticed that sometimes the last letter would get cut off. Is there a way to make sure that the first and last letter never get placed outside of the bounding box?
in this case you can use one function which pass your words randomly to the script to display..
i think this is better option.
instead to work wit

Simple regular expression using preg_match

I am trying to retrieve the three digit number after this word and semi colon.
REF: 222
The code i have below works but its not good because its getting 3digit numbers from the $decoded_message string.
What i really want is only to grab three digit number after the word REF: ###
if (preg_match("([0-9]{3})", $decoded_message, $matches)) {
echo "Match was found <br />";
echo "ref = ".$matches[0];
}
Thanks in advance
You can search for REF: [0-9]{3} and then remove the REF: part.
if (preg_match("/REF: [0-9]{3}/", $decoded_message, $matches)) {
echo "Match was found <br />";
echo "ref = ".substr( $matches[0], 5 );
}
You may simply replace "REF: " by using
$output = preg_replace("/REF: /","", "REF: 222");
Afterwards, only the number should be contained in $output.

Resources