while-loops and if-statement trouble php - php

I want to be able to check wheter an inputted string has any of the characters in $alphabet and if it does display the relevant image per relevant character. so if someone entered hello. it would display h.png, e.png, l.png, l.png and o.png. So far I have got it to recognise the users input and echo it out and search whether it has a particular letter and output it to the relevant image via this code:
<?php
$input = trim($_POST["textarea"]);
echo $input;
echo "<br />";
if(strcmp($input[0],'a')==0){
echo "<img src='egypt/$input.png'>";
}else{
echo "You did not write a";
}
?>
Which works perfectly. However I tried to input more code which would allow a whole string including spaces to be analysed against the whole alphabet $alphabet and match each character in the string to the right image with this code below: but it doesnt work
<?php
$input = trim($_POST["textarea"]);
echo $input;
echo "<br />";
$alphabet = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
while (list(, $value) = each($alphabet) AND list(, $input) = each($value2)) {
if(strcmp($value2[0],$value)==0){
echo "<img src='egypt/$value2.png'>";
}else{
echo "You did not write a";
}
}
?>
Above is the neccessary code.
Update: I have worked out how to match the first letter of input against any in the alphabet but still struggling to work out how to map against a whole string with spaces.
<?php
$input = trim($_POST["textarea"]);
echo $input;
echo "<br />";
$alphabet = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
if(strcmp($input[0],$alphabet)==0){
echo "<img src='egypt/$input.png'>";
}else{
echo "Error";
}
?>
Code with reference to update for anyone who cares

Let me suggest you a different route.
$fos='hello world!';
$cucc=preg_replace('/([a-z])/', '<img src="$1.png" />', $fos);
This will replace every letter (a-z) with its img equivalent, so you will get this result:
<img src="h.png" /><img src="e.png" /><img src="l.png" /><img src="l.png" /><img src="o.png" /> <img src="w.png" /><img src="o.png" /><img src="r.png" /><img src="l.png" /><img src="d.png" />!

I'm not sure if this will solve your problem completely, but I noticed this right away:
while (list(, $value) = each($alphabet) AND list(, $input) = each($value2))
should be:
while (list(, $value) = each($alphabet) AND list(, $value2) = each($input))

Related

extract pure text strings from php scripts for translation

I have project which contains bigger amount of php files. Former programmer wrote everything (texts) in english in source files together with html code and I need to make translation now. Go manually file by file and extract all texts to one lanugage file is huge pain. Is there any free tool please to extract and convert all text to e.g. variables in source files and produce just one big file with text variables to simple translation?
many thanks.
P.S. I would like to automatize this work rather than manually do it file-by-file.
Examples of code in php files:
<?php
echo "Hi back, " . $user;
?>
<center class="title">No list(s) available.</center>
<tr id="exp<?php echo $r; ?>" class="me" onmouseover="dis('<?php echo $u; ?>');"> <td>This is new statement</td></tr>
this function is gonna help you in some cases and it returns the plain text between > and <
before you start you need to replace
' (quotation)
with
\' (backslash quotation)
$text = '
<?php
echo "Hi back, " . $user;
?>
<center class="title">No list(s) available.</center>
<tr id="exp<?php echo $r; ?>" class="me" onmouseover="dis(\'<?php echo $u; ?>\');"> <td>This is new statement</td></tr>
';
the function is:
function getSentences($string){
$arr = array();
$parts = explode(">", $string);
if(count($parts) > 2){
$pattern = "/\>(.*?)</";
foreach($parts as $part){
$part = ">" . $part;
preg_match($pattern, trim($part), $matches);
if(!empty($matches[1]) AND $matches[1] != " "){
if(preg_match('/^[a-zA-Z0-9]/', $matches[1])){
$arr[] = $matches[1];
}
}
}
}else{
$pattern = "/\>(.*?)</";
preg_match($pattern, $string, $matches);
$arr[] = $matches[1];
}
return $arr;
}
and call the function by :
print_r(getSentences($text));
the output will be something like this:
Array ( [0] => No list(s) available. [1] => This is new statement )

PHP echo name+var

I basically have to create a website that display 3 random items out of a total of 10. I have named each of them $item# (# being a number 1-10) and I'm trying to display them using echo.
This is how I have the content stored
$item1 = '<img src="Images/coffee1.png" class="imgLeft" />
<h3>Title 1</h3>
<p>
L.
</p>';
I used this to create a variable with a random int between 1-10
$f1 = rand(1, 10);
And I'm trying to display it using this
<?php echo $item; ?>
If I put a number after item it works perfectly, but I can't figure out how to put $f1 instead.
Thank you
First of all you have to declare your random number before inserting into your Item. if you do it after then your Item doesn't reconize it.
Then in php we can use a concatenation of vars like $f1 . $f2 and it the same when we use it with String type so your Item is a String concatenated with an Integer but we want it inside the string.
In conclusion we have to concatenate String like this `
"Your String ".$1."The rest of String"; OR 'Your String '.$1.'The rest of String';
`
$f1 = rand(1, 10);
$item1 = '<img src="Images/coffee'. $f1 .'.png" class="imgLeft" />
<h3>Title '. $f1 .'</h3>
<p>L.</p>';
<?php echo $item; ?>
Here is how it will work...
$f1 = rand(1, 10);
$$f1 = '<img src="Images/coffee'. $f1 .'.png" class="imgLeft" />
<h3>Title '. $f1 .'</h3>
<p>
L.
</p>';
echo $$f1;
My last edited Answer Now:
It is working...
$f1 = rand(1, 10);
$item = '';
$ite = $item.$f1;
$ite = '<img src="images/'.$f1.'.png" class="imgLeft" /><h3>Title '. $f1 .'</h3><p>L.</p>';
echo $ite;

PHP: preg_match not working

I have the following code:
$data = "Normal text
&nbsp&nbsp&nbsp&nbspcode
&nbsp&nbsp&nbsp&nbspcode
&nbsp&nbsp&nbsp&nbspcode
Normal text";
$data = nl2br($data);
$data= explode('<br />', $data );
foreach($data as $value){
if(preg_match('/^&nbsp&nbsp&nbsp&nbsp/',$value)){
echo 'code';
echo '<br />';
}else{
echo 'Not code';
echo '<br />';
}
}
I want to check if each of the lines starts with 4 spaces and if it does i want to echo as 'Code' and if it doesn't i want to echo as 'Not code'. But i am getting the output as 'Not code' though the 2nd , 3rd and 4th lines start with four spaces. I cannot figure out what I have done wrong. Please help me.
nl2br doesn't generate <br /> unless you tell it to. Your explode logic is wrong.
Instead, try this:
$data = "Normal text.......";
foreach(explode("\n",$data) as $line) {
// your existing foreach content code here
}
got it working
added a trim() to get rid of the newline in front of the string
nl2br replace \n with <br />\n (or <br />\r\n),
so when spliting on <br /> the \n is left as the first char
<?php
$data = "Normal text
&nbsp&nbsp&nbsp&nbspcode
&nbsp&nbsp&nbsp&nbspcode
&nbsp&nbsp&nbsp&nbspcode
Normal text";
$data = nl2br($data);
$data= explode('<br />', $data );
foreach($data as $value)
{
if(preg_match('/^&nbsp&nbsp&nbsp&nbsp/', trim($value)))
{
echo 'code';
}
else
{
echo 'Not code';
}
echo '<br />';
}
?>
You could also use "startsWith" as defined here...
https://stackoverflow.com/a/834355/2180189
...instead of the regexp match. That is, if the spaces are always at the beginning

php strpos not working while searching <br /><br /> after nl2br()

I'm having trouble using strpos correctly. if I search for<br /> it will find it. If I search for <br /><br /><br /> with or without space between, it won't and using htmlspecialchars I can tell the string is full of it.
<?php
$picArray = glob('projectData/' . $data['folder'] . '/*.jpg',GLOB_BRACE);
$text = nl2br($data['definition']).'<br />';
$cutP = 0;
foreach($picArray AS $insert) {
if(strpos($text,'<br /> <br /> <br />',$cutP) !== FALSE){
$cutP = strpos($text,'<br /> <br /> <br />',$cutP)+6;
echo $cutP.'_';
$str_to_insert = '<img class="inTextImg" title="int" src="'.$insert.'" />';
$text = substr($text, 0, $cutP) . $str_to_insert . substr($text, $cutP);
}
else {
echo 'haha';
$text .= '<img class="inTextImg" title="outText!" src="'.$insert.'" />';
}
}
?>
Thank your for your ideas.
This is because nl2br keeps the original line break characters in place, just after the '<br />'. You need to include the line break characters in the string to search for. Since there can be a few different patterns for this it's easiest to use a regexp to match it:
$text = preg_replace('/(?:<br \/>\r?\n?){3}/', $str_to_insert, $text);
Have you tried using preg_match() ?
if(preg_match("\(<br />)+\",$text) > 0){
// code
}
Not 100% on the regex, but you would want one that checks for one or more br tags

echo image url with tags in php

I have previously asked a question on how to echo the url of images from an html page. I can do this successfully but how can I narrow this down further so only images urls beginning with a certain phrase are shown, furthermore how can I add an image tag around them so the images are displayed as images and not just text?
e.g I only want to list images beginning with http://photos.website.com.
edit: I forgot to mention this is the code used to iterate through the images:
foreach($images as $image) {
echo $image->getAttribute('src') . '<br />';
}
You will have to add a condition that tests the content of $image->getAttribute('src').
To test if a string beings by another, a possibility is to use the strpos function, which returns the position of the needle in the haystack -- here, you want that position to be 0 (i.e. the first character of the string).
foreach($images as $image) {
$url = $image->getAttribute('src');
if (strpos($url, 'http://photos.website.com') === 0) {
echo $url . '<br />';
}
}
Simple:
foreach ($images as $image) {
$src = $image->getAttribute('src');
if (stripos($src, 'http://photos.website.com') === 0)
{
echo $src . '<br />';
}
}
And to add tags:
foreach ($images as $image) {
$src = $image->getAttribute('src');
if (stripos($src, 'http://photos.website.com') === 0)
{
echo sprintf('<img src="%s" alt="" />', $src) . "\n";
}
}

Categories