Shorten String function not working properly [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have this little function to stop a string being too long but it dosen't seem to work. I'm assuming i've done something wrong?
function trimString($string, $maxChar) {
$string = (strlen($string) > $maxChar) ? substr($string,0,$maxChar).'...' : $string;
}
I was using it like this:
echo trimString($row['mainTitle'], 30);
Thanks

You forgot to return value from function. Try
function trimString($string, $maxChar) {
return (strlen($string) > $maxChar) ? substr($string,0,$maxChar).'...' : $string;
}

Related

Dynamic Matrices in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have written the PHP code below in order to create a dynamic matrix with different count of columns per row but PHPStorm says the row variable is not defined. please help.
class reply
{
public $text;
private $row = array(array());
private $rowIndex = 0;
private $colIndex = 0;
public function Add($menu)
{
$this->$row[$this->rowIndex][$this->colIndex] = $menu;
$this->colIndex++;
}
public function NextRow()
{
$this->rowIndex++;
$this->colIndex = 0;
}
}
$this->$row is incorrect, it should be $this->row.

PHP String Replace for BBCode [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I want to make a custom BBCode for my forum site, but I've run into an issue, and I'm having a hard time fixing it.
This is what's in the database for the body of the thread "[b]Bold[/b][i]Italic[/i][strike]Strike[/strike]".
However the output is displayed like this "[i]Italic[/i][strike]Strike[/strike]".
So, I'm guessing it's an issue with echoing it out, but i'm not sure how to fix it. Here's the current code:
function bbcode($input) {
$input = strip_tags($input);
$input = htmlentities($input);
$search = array('/\[b\](.*?)\[\/b\]/is');
$replace = array('<b>$body</b>');
return preg_replace($search, $preg_replace, $input);
}
while($row = mysql_fetch_array($threadquery, MYSQL_ASSOC)) {
$body = str_replace("\n",'<br>', $row['body']);
}
echo bbcode($body);
proper code should be:
$replace = array('<b>$1</b>');
return preg_replace($search, $replace, $input);

File_get_contents doesn't import php? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have this code:
elseif($pagina == "agenda")
{
$html = file_get_contents("template/index.php");
$html = str_replace("%content%",file_get_contents("template/agenda.php"),$html);
$html = str_replace("%current2%",'class="current"',$html);
$html = str_replace("%sidebar%",file_get_contents("template/sidebar/sidebar_nieuws.php"),$html);
}
On the page "agenda.php" is a normal php script. This does work when I open agenda.php directly, but when I open it with this code, the code doesn't work. I think file_get_contents doesn't work on php? What is an alternative?
(Sorry for my bad english ;)..)
I would use output buffering. It's effective in this kind of scenario.
$html = file_get_contents("template/index.php");
$arr = array();
ob_start();
include_once('template/agenda.php');
$file = ob_get_contents(); //$file now stores the rendered output (after PHP has run) of template/agenda.php
ob_end_clean();
$arr['agenda'] = $file;
$html = str_replace("%content%", $arr['agenda'], $html);
if you want to run the php code. Then you can include your php file:
include('path/to/file.php');
if you want to change php code from file before executing then in the end run eval($html);

What is wrong with strpos()? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Have code:
$url = 'http://www.domain.com/dir1/dir2/dir3/page-2.html';
if ($strpos = strpos('/page', $url)) echo '1';
else echo '2';
It shows only '2'.
How can I fix it?
Change arguments places
strpos($url, '/page')
strpos
Actually the syntax for function was mismatched I think.
use this
$stropos = stripos($url,"/page");
echo $strpos;
Please add === identical compare in if condition and correct the position of strpos parameters
$url = 'http://www.domain.com/dir1/dir2/dir3/page-2.html';
if ($strpos == strpos($url, '/page')) echo '1';
else echo '2';

$variable = $url + static_page.php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I would like to uplaod files using cURL but can't figure out how I can use $url array.
For example:
$urls = array("http://images.domain.com/",
"http://flash.domain.com/",
"http://other.domain.com/"
);
foreach ($urls as $url) {
I'm trying this but without success:
$upload = "$url ."upload/upload.php";
Any advice? :)
Thanks
You need to take out the first quote for it to work
$urls = array("http://images.domain.com/",
"http://flash.domain.com/",
"http://other.domain.com/"
);
foreach ($urls as $url) {
$upload = $url ."upload/upload.php";
}

Categories